parse out bracketed [L=blah.htm]link text here[/L]

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
matstars
Forum Newbie
Posts: 7
Joined: Tue Aug 12, 2008 2:47 pm

parse out bracketed [L=blah.htm]link text here[/L]

Post by matstars »

What's up.

I'm trying to parse out if a user puts in [L=link.php]Anchor Text[/L] or [L=link.htm]Anchor Text[/L] etc. and convert it to <a href="link.htm">Anchor Text</a>

I tried this - but it seems as if I need a way to get word one get everything until it sees the [/L]

how would I do that?

Code: Select all

 
 
<?php
  $txt='The quick brown fox? [L=Link.htm]Jumped Over the Lazy[/L] Dogs, Now is the good time[L=arga.agr]for men to come to the aid of their country[/L]';
  $re1='(\\[L=)';   # Any Single Character 1
  $re4='((?:[a-z][a-z\\.\\d_]+)\\.(?:[a-z\\d]{3}))(?![\\w\\.])';    # File Name 1
  $re5='(\\])'; # Any Single Character 4
  $re6='(.*)';  # Word 1
  $re8='(\\[\\/L\\])';  # Square Braces 1
 
  if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8."/is", $txt, $matches))
  {
      $matcher="/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8."/is";
      $c1=$matches[1][0];
      $c2=$matches[2][0];
      $c3=$matches[3][0];
      $file1=$matches[4][0];
      $c4=$matches[5][0];
      $word1=$matches[6][0];
      $sbraces1=$matches[7][0];
      
      echo "c1->$c1<BR>";
      echo "c2->$c2<BR>";
      echo "c3->$c3<BR>";
      echo "$file1->$file1<BR>";
      echo "c4->$c4<BR>";
      echo "word1->$word1<BR>";
      echo "sbraces1->$sbraces1<BR>";
      
      $replacement="\n\n\n<a href=\"$c2\">$file1</a>";
      $txt=preg_replace($matcher, $replacement, $txt);
 
  }
echo "<BR><BR>txt-><BR><BR>";
echo $txt;
 
?>
 
Any help?

Thanks!!
Last edited by matstars on Mon Feb 09, 2009 2:37 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: parse out bracketed [L=blah.htm]link text here[/L]

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
matstars
Forum Newbie
Posts: 7
Joined: Tue Aug 12, 2008 2:47 pm

Re: parse out bracketed [L=blah.htm]link text here[/L]

Post by matstars »

astions wrote:Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax][/quote]

I appreciate the help, but I am not quite sure if you understand my question, maybe reading it again would help?

Thanks!

Mat
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: parse out bracketed [L=blah.htm]link text here[/L]

Post by prometheuzz »

matstars wrote:What's up.

I'm trying to parse out if a user puts in [L=link.php]Anchor Text[/L] or [L=link.htm]Anchor Text[/L] etc. and convert it to <a href="link.htm">Anchor Text</a>

...
Something like this:

Code: Select all

<?php
$text = 'The quick brown fox? [L=Link.htm]Jumped Over the Lazy[/L] Dogs, Now is 
the good time[L=arga.agr]for men to come to the aid of their country[/L]';
echo preg_replace(
  '#\[L=([^]]*)]((?:(?!\[/L]).)*)\[/L]#is', 
  '<a href="$1">$2</a>', 
  $text
);
?>
?
matstars
Forum Newbie
Posts: 7
Joined: Tue Aug 12, 2008 2:47 pm

Re: parse out bracketed [L=blah.htm]link text here[/L]

Post by matstars »

prometheuzz wrote:
matstars wrote:What's up.

I'm trying to parse out if a user puts in [L=link.php]Anchor Text[/L] or [L=link.htm]Anchor Text[/L] etc. and convert it to <a href="link.htm">Anchor Text</a>

...
Something like this:

Code: Select all

<?php
$text = 'The quick brown fox? [L=Link.htm]Jumped Over the Lazy[/L] Dogs, Now is 
the good time[L=arga.agr]for men to come to the aid of their country[/L]';
echo preg_replace(
  '#\[L=([^]]*)]((?:(?!\[/L]).)*)\[/L]#is', 
  '<a href="$1">$2</a>', 
  $text
);
?>
?
I get Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in /home/billmat/public_html/max/admin/test.php on line 3

with

Code: Select all

 
 $text = 'The quick brown fox? [L=Link.htm]Jumped Over the Lazy[/L] Dogs, Now is the good time[L=arga.agr]for men to come to the aid of their country[/L]';
 echo preg_replace('\[L=([^]]*)]((?:(?!\[/L]).)*)\[/L]is','<a href="$1">$2</a>',$text);
 
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: parse out bracketed [L=blah.htm]link text here[/L]

Post by prometheuzz »

matstars wrote:
prometheuzz wrote:
matstars wrote:What's up.

I'm trying to parse out if a user puts in [L=link.php]Anchor Text[/L] or [L=link.htm]Anchor Text[/L] etc. and convert it to <a href="link.htm">Anchor Text</a>

...
Something like this:

Code: Select all

<?php
$text = 'The quick brown fox? [L=Link.htm]Jumped Over the Lazy[/L] Dogs, Now is 
the good time[L=arga.agr]for men to come to the aid of their country[/L]';
echo preg_replace(
  '#\[L=([^]]*)]((?:(?!\[/L]).)*)\[/L]#is', 
  '<a href="$1">$2</a>', 
  $text
);
?>
?
I get Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in /home/billmat/public_html/max/admin/test.php on line 3

with

Code: Select all

 
 $text = 'The quick brown fox? [L=Link.htm]Jumped Over the Lazy[/L] Dogs, Now is the good time[L=arga.agr]for men to come to the aid of their country[/L]';
 echo preg_replace('\[L=([^]]*)]((?:(?!\[/L]).)*)\[/L]is','<a href="$1">$2</a>',$text);
 
That is because you removed the delimiters from my example.
Post Reply