Solved

Regex to replace <[content]>

Posted on 2008-10-22
4
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

635 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