Solved

Regex to replace <[content]>

Posted on 2008-10-22
4
206 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now