Solved

Regex to replace <[content]>

Posted on 2008-10-22
4
210 Views
Last Modified: 2008-10-22
I want to find this or any acceptable variation of it:

       <[content]>

And replace it with:

       xyz

This should be case insensitive and should allow ANYTHING between "content" and the brackets.

So all these should be replaced:

       <[content]>
       <[CONTENT]>
       <[*ContenT*]>
       <[ content  / ]>

This must be PHP4 compatible.
0
Comment
Question by:hankknight
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22780616
This will work in most cases:
$searchFor = "content";
$replaceWith = "xyz";
echo preg_replace( "#<\[.*?$searchFor.*?\]>#i",$replaceWith, $str);

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 300 total points
ID: 22780619
try:
preg_replace('#<\[[^c]*(content)[^\]]*\]>#i','xyz',$str);

Open in new window

0
 
LVL 1

Expert Comment

by:cristides
ID: 22780629
this is one example for check if on if is valid or not.
ereg("([12]?[0-9]?[0-9].[12]?[0-9]?[0-9].[12]?[0-9]?[0-9].[12]?[0-9]?[0-9])", $dirty, $clean);

0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 200 total points
ID: 22780751
One problem case with my code:

$searchFor = "content";
$replaceWith = "xyz";
$str = "Here is some <[*blah*]> and some content and some more <[*blah*]> and some <[234content!!]> for good measure";
echo "str before: $str<br>\n";
$str = preg_replace( "#<\[.*?$searchFor.*?\]>#i",$replaceWith, $str);
echo "str after: $str<br>\n";

Output:

str before: Here is some <[*blah*]> and some content and some more <[*blah*]> and some <[234content!!]> for good measure
str after: Here is some xyz and some xyz for good measure

A slight modification very similar to hielo's suggestion, will fix this case:

$searchFor = "content";
$replaceWith = "xyz";
$str = "Here is some <[*blah*]> and some content and some more <[*blah*]>";
echo "str before: $str<br>\n";
$str = preg_replace( '#<\[[^\]]*?'.$searchFor.'[^\]]*?\]>#i',$replaceWith, $str);
echo "str after: $str<br>\n";

which correctly outputs:

str before: Here is some <[*blah*]> and some content and some more <[*blah*]> and some <[234content!!]> for good measure
str after: Here is some <[*blah*]> and some content and some more <[*blah*]> and some xyz for good measure

But that still misses this case with an extra ] character:

$str = "Here is some <[*blah*]> and some content and some more <[*blah*]> and some <[234content! ] ]> for good measure";

Output is:

str before: Here is some <[*blah*]> and some content and some more <[*blah*]> and some <[234content! ] ]> for good measure
str after: Here is some <[*blah*]> and some content and some more <[*blah*]> and some <[234content! ] ]> for good measure

Is that going to be good enough for the job, or do you need that last case covered too?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question