Jump to content

php Regex Question...


phpSensei

Recommended Posts

I am terrible at understanding, and writing regular expressions, and using preg_match(); .

 

I need some code that can detect"<TAG> string </END TAG>", and replaces the <TAG> and </Tag> with something of my own...

 

basically, this is for a ["b"][/"b"], and ["url"=""][/url] code I need for my forum....

Link to comment
Share on other sites

Here's a quick example. You can find more by searching the forums for "bbcode".

 

<pre>
<?php
$string = '<TAG>stuff</TAG>';
echo preg_replace('#<([^>]+)>(.*?)</\1>#', '[b]$2[/b]', $string);
?>
</pre>

 

lol thanks, but i dont understand a word of it. @$#@$#@$#@%$#%$^%$$&%D>>>>

Link to comment
Share on other sites

Even after reading through the manual?

 

Here's an explanation from Perl's YAPE::Regex::Explain:

NODE                    EXPLANATION

----------------------------------------------------------------------

  <                        '<'

----------------------------------------------------------------------

  (                        group and capture to \1:

----------------------------------------------------------------------

    [^>]+                    any character except: '>' (1 or more

                            times (matching the most amount

                            possible))

----------------------------------------------------------------------

  )                        end of \1

----------------------------------------------------------------------

  >                        '>'

----------------------------------------------------------------------

  (                        group and capture to \2:

----------------------------------------------------------------------

    .*?                      any character except \n (0 or more times

                            (matching the least amount possible))

----------------------------------------------------------------------

  )                        end of \2

----------------------------------------------------------------------

  </                      '</'

----------------------------------------------------------------------

  \1                      what was matched by capture \1

----------------------------------------------------------------------

  >                        '>'

----------------------------------------------------------------------

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.