SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expressions Question

    Hi All

    I'm not great at regular expressions need help please. I'll try to simplify my aim by using a lame piece of my own code below :'( .

    Basically if I want to extract the 'phpnoob' out of the following string '[curl]phpnoob[curl]', what is the correct procedure, or rather how the heck do I get to split this thing in 3 and get only a string with "phpnoob" (and even possibly the "[curl]" etc. Splits wouldd'nt work because there will be plenty more text in the string so I have to find and replace using a match with regular expression.
    Ereg() would'nt work either.

    Please keep in mind I've naturally tried various variations of this so don't waste my and you're time adding another variation of my code in there

    $String = "[curl]phpnoob[/curl] & other nonsense";
    $link_pat = "^[curl](.+)[/curl]$";
    $repl = "<a href=\\1>\\1</a>";

    $post_text = ereg_replace($link_pat, $repl, $post_text);

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if this is exactly what you're after, but it will break up the string into [curl], phpnoob, and [/curl] (and matches[0] will contain the entire capture, [curl]phpnoob[/curl].

    Here's a quick and dirty test of the pattern I cooked up:

    PHP Code:
    $string "[curl]phpnoob[/curl] & other nonsense";

    preg_match('/(\[curl\])(.+)(\[\/curl\])/'$string$matches);
    foreach(
    $matches as $match) {
        echo 
    $match '<br />';


  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And just for fun, how bout this:

    *Edit: original code left "& other nonsense" in the replaced string.

    PHP Code:
    $string "[curl]phpnoob[/curl] & other nonsense";
    $pattern "/(\[curl\])(.+)(\[\/curl\])(.*)/i";
    $repl "<a href="\\2">\\2</a>";
    echo 
    preg_replace($pattern$repl$string); 

  4. #4
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kong u are the king man!. After careful examination of you're code my error stands out like an alien does'nt it . Thx dude stay cool dude<br>

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad I could help.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •