Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regex to replace <[content]>

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

Technology Partners: 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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 …

660 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