Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regex to replace <[content]>

Posted on 2008-10-22
4
Medium Priority
?
215 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 1200 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 800 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

Industry Leaders: 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.
Suggested Courses

963 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