Helpful Information
 
 
Category: Regex Programming
Replace all occurances of chars in string

I am new to RegEx and simply want to replace all occurrences of certain characters in a string while retaining the replaced character's case, for instance, if my string is "Anangelo" and I replace "an" with "<b>an</b>", I want the result to be :

<b>An</b><b>an</b>gelo

Any ideas?

I am new to RegEx and simply want to replace all occurrences of certain characters in a string while retaining the replaced character's case, for instance, if my string is "Anangelo" and I replace "an" with "<b>an</b>", I want the result to be :

<b>An</b><b>an</b>gelo

Any ideas?

You can use preg_replace() function.

Example :



<?php
$str = "Anangelo";

// CASE SENSITIVE //
$output = preg_replace("/An/","<b>An</b>",$str);

// IGNORE CASE SENSITIVE //
$output = preg_replace("/An/i","<b>An</b>",$str);

?>


I think you are looking somethin like this only ?

You can use preg_replace() function.

Example :



<?php
$str = "Anangelo";

// CASE SENSITIVE //
$output = preg_replace("/An/","<b>An</b>",$str);

// IGNORE CASE SENSITIVE //
$output = preg_replace("/An/i","<b>An</b>",$str);

?>


I think you are looking somethin like this only ?

Aw man, sorry; I should have specified that I need to do this using JavaScript.










privacy (GDPR)