#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    57

    Don't parse links when inside this?


    Basically i have this regex to parse links in a post:
    Code:
    #(http://){1}((www\.)?[a-z][a-z0-9_.-]*\.[a-z]{2,6}[a-zA-Z0-9/.?&%-]*)(\.com|org|us|uk|nl|de|info|me)([a-zA-Z0-9=\;\_\\/\\.\\?&%-]*)#i
    How do i adjust it so it won't parse anything wrapped in:
    Code:
    [img][/img]
    Any help would be grand!
  2. #2
  3. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Simply add
    Code:
    (?!\[/img])
    at the end of your regex (it's called a negative look ahead, btw).
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    57
    Thanks, i added it into it so it now looks like this:
    Code:
    #(http://){1}((www\.)?[a-z][a-z0-9_.-]*\.[a-z]{2,6}[a-zA-Z0-9/.?&%-]*)(\.com|org|us|uk|nl|de|info|me)([a-zA-Z0-9=\;\_\\/\\.\\?&%-]*)(?!\[/img])#i
    It still seems to try and parse it though so my image code ends up like this:
    Code:
    <img src="<a href="http://www.prxa.info/area51/templates/utopia/images/logoleft.jpg">http://www.prxa.info/area51/templates/utopia/images/logoleft.jpg</a>" alt="" />
    I am guessing i added it in wrong?
  6. #4
  7. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by liamdawe
    ...

    I am guessing i added it in wrong?
    No, you didn't. Your regex will now match only url's NOT directly followed by "[/img]", which is what you asked for, AFAIK. If "it doesn't work", please provide enough information (1. actual input, 2. actual output and 3. desired output) to get help.

    Good luck!
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    57
    Ok sure well actual input is this (without the quotes, just stopping it parsing on here):
    Code:
    "http://www.prxa.info/area51/templates/utopia/images/logoleft.jpg"
    Actual output was this:
    Code:
    <img src="<a href="http://www.prxa.info/area51/templates/utopia/images/logoleft.jpg">http://www.prxa.info/area51/templates/utopia/images/logoleft.jpg</a>" alt="" />
    Desired is this:
    Code:
    <img src="http://www.prxa.info/area51/templates/utopia/images/logoleft.jpg" alt="" />
    To parse the image im doing this:
    Code:
    $this->text = preg_replace('#\[img\](.*?)\[/img\]#mi',"<img src=\"$1\" alt=\"\" />",$this->text);
  10. #6
  11. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    In your original post you said you wanted to parse url's NOT inside img-tags. Yet what you post now is the complete opposite: you're parsing url's inside img-tags.

    I'm logging out for tonight, I'll have a look at this thread tomorrow. I'd like to see a proper problem description (preferably with some decent examples), otherwise I'm afraid I can't help.

    Good luck.
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    57
    What i said is true, i have a parser which currently will parse html links.

    I don't want that to parse a link if it is included inside img tags because i want the image parser side to do that.
  14. #8
  15. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by liamdawe
    What i said is true, i have a parser which currently will parse html links.

    I don't want that to parse a link if it is included inside img tags because i want the image parser side to do that.
    I still have no idea what exactly you're trying to do. The few examples you've provided have not helped me to figure this out.

    No need to explain yourself further to me perhaps someone else can help you.

    Best of luck of course.
    Last edited by prometheuzz; June 12th, 2009 at 02:32 AM.
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    57
    I don't know how this is hard to understand?

    I have a link parser which turns
    Code:
    http://ww....
    Into a html link.

    I Don't want it to turn it into a link if the url is wrappin in img tags, it's that simple like i tried explaining.
  18. #10
  19. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by liamdawe
    I don't know how this is hard to understand?
    ...
    It isn't all that complex, but IMO you explained your problem very poorly. Especially with regex advice it is very important to see exactly what you are doing and in what way things are going wrong.
    After you said that my first reply didn't work (without providing the exact code you used) and I asked for calcification, you then provided some sample code where you actually tried to match urls INSIDE img-tags which lead me to believe you were trying to do the exact opposite of what you described in your original post. You are also using the word "url" to describe a web-address (http://...), but also as an html anchor which also does not help clarify your explanation.

    It may very well be clear to you, but realise that posting your problem on a public forum you will have to explain your problem very precisely.

    Anyway just to prove that my first suggestion was the correct answer, here's a snippet of code:

    PHP Code:
    $text 'text http://abc bla bla [img]http://def[/img] more 
    text http://ghi bla bla [img]http://jkl[/img]'
    ;
    preg_match_all('#http://[a-z]+\b(?!\[/img])#i'$text$matches);
    print_r($matches);
    /* output:
    Array
    (
        [0] => Array
            (
                [0] => http://abc
                [1] => http://ghi
            )

    )
    */ 
    As you can see, http://abc and http://ghi are NOT inside img-tags and they get matched because of the negative look ahead.

    Also notice that I posted a small piece of code that you can actually copy, paste and run without making any modifications to it (contrary to your code posted in this thread). This is always the preferred way of posting code on a forum: that way anyone can see exactly what you're seeing and will leave less room for speculations.

    I hope you can see it from my point of view.

    Good luck.
  20. #11
  21. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    57
    Sorry i really have never been very good at explaining things i do need to apologise for that i'm a little slow with certain things unfortunatly.

    When i say url i mean a web address, currently when an address is entered it will be turned into a html equivalent (so it then becomes a clickable link).

    What i wish to do is when people add img tags around an address turn that into the html for an image.

    I really hope that is a little better. I can deffinately see from your point of view and i really appreciate your replys.

    What i want is to adjust the original regex to not parse the web address if it is wrapped in img tags.

    The actual current code for it is pretty much this:
    PHP Code:
    $this->text preg_replace($pattern'<a href="$0">$0</a>'$this->text); 
    Is the preg match a better way to do it?
    Last edited by liamdawe; June 13th, 2009 at 01:54 PM.
  22. #12
  23. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by liamdawe
    Sorry i really have never been very good at explaining things i do need to apologise for that i'm a little slow with certain things unfortunatly.

    When i say url i mean a web address, currently when an address is entered it will be turned into a html equivalent (so it then becomes a clickable link).

    What i wish to do is when people add img tags around an address turn that into the html for an image.

    I really hope that is a little better. I can deffinately see from your point of view and i really appreciate your replys.

    What i want is to adjust the original regex to not parse the web address if it is wrapped in img tags.
    Okay, no problem.
    Now, did you run my code? Does it do what you want? If both are answered by a "yes", then what exactly is the problem?
    If you still have a problem, please post a snippet of code as I described: something that I can copy, paste and run so that I see exactly what you see (without needing to change anything!). Help me help you!
  24. #13
  25. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    57
    I tested with this text entered (without the quotes, just stopping it parsing on here):
    Code:
    [img]"http://www.prxa.info/area51/templates/utopia/images/logoleft.jpg"[/img]
    http://www.prxa.info
    And then this to parse:
    PHP Code:
    $pattern "#http://[a-z]+\b(?!\[/img])#i";
    preg_match_all($pattern$this->text$matches);
    print_r($matches); 
    Also my img code:
    PHP Code:
    $this->text preg_replace('#\[img\](.*?)\[/img\]#mi',"<img src=\"$1\" alt=\"\" />",$this->text); 
    Where "$this->text" is the first set of addresses.

    And it gave me this:
    Code:
    Array ( [0] => Array ( [0] => http://www [1] => http://www ) )

    Comments on this post

    • prometheuzz disagrees : You really should read what pepple trying to help you ask of you.
  26. #14
  27. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    * You didn't answer any of my questions from my previous reply;
    * You didn't provide a snippet I can simply copy, paste and run without needing to change anything;
    * You didn't explain what exactly is wrong.

    I'm out.

    Comments on this post

    • liamdawe disagrees : I showed exactly what was needed, ugh some people like you can never be pleased!
    Last edited by prometheuzz; June 13th, 2009 at 02:08 PM.
  28. #15
  29. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    57
    Ugh fine im out too i've done the best i can and i posted code you can use.

IMN logo majestic logo threadwatch logo seochat tools logo