Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Well,

I've some texts like this below:

< Jens > is my name. I play < Football >. I saw < Steffy > Yesterday. Yeah, We will be < Together > For sure.

And I just want all the texts between '<' & '>' (including <>) to be bold programmatically using Regular Expression (Preferably) or any other method. This is a kind of Find & Replace. So after the operation texts should be :

< Jens > is my name. I play < Football >. I saw < Steffy > Yesterday. Yeah, We will be < Together > For sure.

share|improve this question
    
try <[^<>]*> regex. –  Avinash Raj Aug 27 at 10:39

3 Answers 3

up vote 1 down vote accepted

Use preg_replace_callback():

<?php
// header('Content-Type: text/plain; charset=utf-8');

$test = <<<TXT
< Jens > is my name. I play < Football >.
I saw < Steffy > Yesterday. Yeah, We will be < Together > For sure.
TXT;

$result = preg_replace_callback(
    '/<[^>]+>/',
    function($matches){
        return '<b>' . htmlspecialchars($matches[0]) . '</b>';
    },
    $test
);

print_r($result);
?>

Output:

< Jens > is my name. I play < Football >. I saw < Steffy > Yesterday. Yeah, We will be < Together > For sure.

share|improve this answer
    
Why need a callback? Wouldn't preg_replace and as replacement '<b>\0</b>' be convenient? –  Jonny 5 Aug 27 at 10:44
    
It gets rendered in browser just like this : <b>< Jens ></b> is my name. I play <b>< Football ></b>. I saw <b>< Steffy ></b> Yesterday. Yeah, We will be <b>< Together ></b> For sure. No bold! –  Maxin Bits Aug 27 at 10:45
    
@Jonny5 I do have a strong feeling, that after this "Find & Replace" OP might want to have more control over situation. –  HAL9000 Aug 27 at 10:45
    
@MaxinBits remove header() string –  HAL9000 Aug 27 at 10:46
    
ThankYou..Cheers..:) –  Maxin Bits Aug 27 at 10:51

For Much better understanding and learning regex for the further work you can visit the below links

Learning Regular Expressions

Useful regular expression tutorial

Regular expressions tutorials

And one of the best and easy one and my favourite is

http://www.9lessons.info/2013/10/understanding-regular-expression.html?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+9lesson+%289lessons%29

very nice and easy tutorial for the beginners

share|improve this answer

You can use this preg_replace:

$repl = preg_replace('/(<[^>]*>)/', '<b>$1</b>', $str);

<b>< Jens ></b> is my name. I play <b>< Football ></b>. I saw <b>< Steffy ></b> Yesterday. Yeah, We will be <b>< Together ></b> For sure.

RegEx Demo

share|improve this answer
    
\b or <b>? I'm on browser not in CLI. –  Maxin Bits Aug 27 at 10:47
    
Yes it should be <b>, corrected in code now. –  anubhava Aug 27 at 10:51
    
ThankYou..:) (<b>$1<\b> --> <b>$1</b>) –  Maxin Bits Aug 27 at 10:56
    
Yes that's correct, my HTML skills are pretty poor :( –  anubhava Aug 27 at 10:58
    
its ok..np..Cheers..:) –  Maxin Bits Aug 27 at 15:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.