Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Regex problem

  1. #1
    New Coder
    Join Date
    Mar 2006
    Location
    I'm lost, livin inside myself
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regex problem

    Okay, I'm creating a mod for some forum software and I'm trying to create a regex string that can search a post for all links and put them into an array.

    Basically, what I have at this point is a variable that has the text in it and I need to search it.

    Links can appear in four different formats

    PHP Code:
    http://www.example.com
    [URL="http://www.example.com"]link[/URL]
    [
    url=http://www.example.com]link[/url]
    [url]http://www.example.com[/url] 
    What I need to do is search a string for any of these formats and put them all into an array. I have next to no experience with regex and I was wondering if someone could help me out.
    $guiness &= new sixpack();
    $guiness->chug();

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by The Reverend View Post
    Links can appear in four different formats

    PHP Code:
    http://www.example.com
    [URL="http://www.example.com"]link[/URL]
    [
    url=http://www.example.com]link[/url]
    [url]http://www.example.com[/url] 
    avoid first format because:
    - I want to be text not link, what I must do?
    - users are smart enought to know do find how to write links if they want
    - is dificult to parse, what if is ftp://.. or others forms

    PHP Code:
    $what = array("/\[url=\"?([^\"\]]+)\"?\]([^\[]+)\[\/url\]/i",
    "/\[url\]([^\[]+)\[\/url\]/i");
    $with = array("$1 $2""$1");
    $links preg_replace($what$with$text); 
    is not tested, debug with print_r($variable) or echo/print.

    for regex details, see:

    http://www.php.net/manual/en/ref.pcre.php

    best regards


  •  

    Posting Permissions

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