Solved

Regular expression woes

Posted on 2002-04-20
2
160 Views
Last Modified: 2008-03-06
I want to find every occurance of a string and be able to replace it with a different string depending on a parameter passed.  (Cryptic, I know)

<mytag=some parameter>stuff inside!</mytag> text inbetween <mytag=param again>more stuff even</mytag>

The problem I am having is that my code will give me "stuff inside!</mytag> text inbetween <mytag=param again>more stuff even" instead of each one seperately.

Here is my code:

$regexp="\[mytag\=([a-zA-Z0-9 ]+)\]([[:graph:] ]+)\[\/mytag\]";
while(eregi($regexp,$message,$regs)){
  if(str_replace(" ","",strtolower("Tom Apps com"))==str_replace(" ","",strtolower($regs[1]))){
    $message=eregi_replace($regexp,"\\2",$message);
  }else{
    $message=eregi_replace($regexp,"",$message);
  }
}
echo $message;

Help if you can! :)
0
Comment
Question by:VBTom
[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 Comments
 
LVL 6

Accepted Solution

by:
andreif earned 100 total points
ID: 6959072
Problem is that program looks for the longest match.
To make it look for the shortest, add ? to * or +

Your RegExp:

$regexp="\[mytag\=([a-zA-Z0-9 ]+)\]([[:graph:] ]+)\[\/mytag\]";

should read as

$regexp="\[mytag\=([a-zA-Z0-9 ]+)\]([[:graph:] ]+?)\[\/mytag\]";
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

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 …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

726 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