This is a discussion on regexp question within the PHP Language forums, part of the PHP Programming Forums category; Hello, In document I have text like this (might be several entries): template(directory,name,link description) and need to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello,
In document I have text like this (might be several entries): template(directory,name,link description) and need to automatically replace it with this: <a href="template.php?dir=directory&tpl=name">lin k description</a> How can i do that? |
|
|||
|
$old_str = "template(directory,name,link description)";
$ar = split("[(,)]",$old_str); $new_str = "<A href='".$ar[0].".php?dir=".$ar[1]."&tpl=".$ar[2].">".$ar[3]."</A>"; "The Dude" <noreply@sourceforge.net> wrote in message news:bk9ahg$qootd$1@ID-101500.news.uni-berlin.de... > Hello, > > In document I have text like this (might be several entries): > > template(directory,name,link description) > > and need to automatically replace it with this: > > <a href="template.php?dir=directory&tpl=name">lin k description</a> > > How can i do that? > > |
|
|||
|
Hi,
It won't work. I don't have that exact line - that text is somewhere inside big text. And there might be more than one entry in that text. Any other suggestions? "sam" <rbaba99@caramail.com> wrote in message news:bk9doi$2lur$1@news.cybercity.dk... > $old_str = "template(directory,name,link description)"; > > $ar = split("[(,)]",$old_str); > > $new_str = "<A > href='".$ar[0].".php?dir=".$ar[1]."&tpl=".$ar[2].">".$ar[3]."</A>"; > > "The Dude" <noreply@sourceforge.net> wrote in message > news:bk9ahg$qootd$1@ID-101500.news.uni-berlin.de... > > Hello, > > > > In document I have text like this (might be several entries): > > > > template(directory,name,link description) > > > > and need to automatically replace it with this: > > > > <a href="template.php?dir=directory&tpl=name">lin k description</a> > > > > How can i do that? > > > > > > |
|
|||
|
"The Dude" <noreply@sourceforge.net> wrote in message news:bk9t5b$ra7c0$1@ID-101500.news.uni-berlin.de... > Hi, > > It won't work. I don't have that exact line - that text is > somewhere inside big text. And there might be more > than one entry in that text. > > Any other suggestions? > > > "sam" <rbaba99@caramail.com> wrote in message > news:bk9doi$2lur$1@news.cybercity.dk... > > $old_str = "template(directory,name,link description)"; > > > > $ar = split("[(,)]",$old_str); > > > > $new_str = "<A > > href='".$ar[0].".php?dir=".$ar[1]."&tpl=".$ar[2].">".$ar[3]."</A>"; > > > > "The Dude" <noreply@sourceforge.net> wrote in message > > news:bk9ahg$qootd$1@ID-101500.news.uni-berlin.de... > > > Hello, > > > > > > In document I have text like this (might be several entries): > > > > > > template(directory,name,link description) > > > > > > and need to automatically replace it with this: > > > > > > <a href="template.php?dir=directory&tpl=name">lin k description</a> > > > > > > How can i do that? > > > -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet? I've read the history and I think you need to give a greater example of the string you're talking about converting - or at least, some "definites" - In the reply to the earlier suggested solution, you've said you don't have the exact line and that "the text is somewhere inside the big text. And there might be more than one entry in that text" Until you manage to provide some examples of what you're talking about I don't think anyone will come close to helping... and remember... so others can help... always reply at the end of the previous post so folk can read the history (oldest at top, most recent at bottom). |
|
|||
|
The Dude wrote:
> In document I have text like this (might be several entries): > > template(directory,name,link description) > > and need to automatically replace it with this: > > <a href="template.php?dir=directory&tpl=name">lin k description</a> Devoid of material details -- such as what constraints bind specific labels -- we can only offer meager nudges in the appropriate directions, and cannot propose fully-fledged practicable solutions. But, hey, this is Usenet: nobody takes the rap. Lacking further elaboration, here's a regexp (including delimiters): #(\w+)\((\w+),(\w+),([\w\s]+)\)# The above pattern matches and captures a "word" (alphanumerics and the underscore ("_"), representable by the following character class [a-zA-Z0-9_]+). Next, it matches a single opening parenthesis. Subsequently, inside the brackets, it matches and captures a "word" followed by a comma; then another word followed by a comma. The pattern then matches and captures one or more "word" characters and/or whitespace characters [1]. Lastly, it matches a single right parenthesis. (Note that there's no need to invert the greediness of the + quantifier in any instance here.) It's then a simple case of planting those captured subpatterns into an anchor element, using backreferences. So, we call on the befitting preg_replace [2] function to manipulate our text: $string = preg_replace( "#(\w+)\((\w+),(\w+),([\w\s]+)\)#", '<a href="$1.php?dir=$2&tpl=$3">$4</a>', $string); Backreferences (the character sequences $1, $2, and so on) refer to the precaptured subpatterns; that is, in your example, $1 contains "template", $2 contains "directory", and so forth. Apologies for the rather pleonastic followup, but I do hope it has shed some light on dark territory. [1] This character class may be insufficient for your purposes. Add to it as you please. You might also allow whitespace between the labels too. I dunno. [2] http://www.php.net/manual/en/function.preg-replace.php -- Jock |
|
|||
|
"The Dude" <noreply@sourceforge.net> wrote in message news:bk9t5b$ra7c0$1@ID-101500.news.uni-berlin.de... > Hi, > > It won't work. I don't have that exact line - that text is > somewhere inside big text. And there might be more > than one entry in that text. > > Any other suggestions? > > > "sam" <rbaba99@caramail.com> wrote in message > news:bk9doi$2lur$1@news.cybercity.dk... > > $old_str = "template(directory,name,link description)"; > > > > $ar = split("[(,)]",$old_str); > > > > $new_str = "<A > > href='".$ar[0].".php?dir=".$ar[1]."&tpl=".$ar[2].">".$ar[3]."</A>"; > > > > "The Dude" <noreply@sourceforge.net> wrote in message > > news:bk9ahg$qootd$1@ID-101500.news.uni-berlin.de... > > > Hello, > > > > > > In document I have text like this (might be several entries): > > > > > > template(directory,name,link description) > > > > > > and need to automatically replace it with this: > > > > > > <a href="template.php?dir=directory&tpl=name">lin k description</a> > > > > > > How can i do that? > > > parse_url() and/or parse_url () might be of some help to do what ever you want to do -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet? |