regexp question

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-17-2003
The Dude
 
Posts: n/a
Default regexp question

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&amp;tpl=name">lin k description</a>

How can i do that?


Reply With Quote
  #2 (permalink)  
Old 09-17-2003
sam
 
Posts: n/a
Default Re: regexp question

$old_str = "template(directory,name,link description)";

$ar = split("[(,)]",$old_str);

$new_str = "<A
href='".$ar[0].".php?dir=".$ar[1]."&amp;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&amp;tpl=name">lin k description</a>
>
> How can i do that?
>
>



Reply With Quote
  #3 (permalink)  
Old 09-17-2003
The Dude
 
Posts: n/a
Default Re: regexp question

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]."&amp;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&amp;tpl=name">lin k description</a>
> >
> > How can i do that?
> >
> >

>
>



Reply With Quote
  #4 (permalink)  
Old 09-17-2003
Randell D.
 
Posts: n/a
Default Re: regexp question


"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]."&amp;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&amp;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).


Reply With Quote
  #5 (permalink)  
Old 09-17-2003
John Dunlop
 
Posts: n/a
Default Re: regexp question

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&amp;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&amp;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
Reply With Quote
  #6 (permalink)  
Old 09-17-2003
Randell D.
 
Posts: n/a
Default Re: regexp question


"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]."&amp;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&amp;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?


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:13 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0