Solved

Regular expression woes

Posted on 2002-04-20
2
152 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

773 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