Jump to content

[SOLVED] The URL replace code is having trouble.


JasonLewis

Recommended Posts

Okay, effigy gave me the following expression to replace urls in a page that are not linked with linked ones. And only if they were not between:

[url][/url]

And:

[url=http://www.somewebsite.com][/url]

 

Now it works fine apart from one thing.

If I have a link like this:

[url=www.website.com]Some text here with space[/url]

 

It will work, which is good.. BUT. If I have this:

[url=http://www.website.com]Some text here with space[/url]

It wont. The http:// throws it off, but if I remove the spaces then it'll work.

 

This is the expression I am using, any suggestions on how I can fix it?

preg_match_all('%
     ### Not preceded by an url start.
     (?<!url[=\]])
     (
        ### Protocol or start.
        (?:
           (??:https?|ftp)://)
           |
           www\.
        )
        ### Body.
        (?>
           ### Gobble all non-space with the exception of [/url]
           (??!\[/url\])\S)+
           ### Avoid ending punctuation.
           (?<!\p{P})
        )
        ### Not followed by an url end.
        (?!\[/url\])
     )
  %x',$str,$matches);

 

Cheers!

 

Okay effigy is much better at RegEx then I,

I am not sure what you mean by linked and unlinked !

 

Okay, effigy gave me the following expression to replace urls in a page that are not linked with linked ones. And only if they were not between:

 

reading effigy regex i would guess you could do this

 

         ### Protocol or start.
         (?:
            (??:https?|ftp)://)
            |
            www\.
         )

to

         ### Protocol or start.
         (?:
            (??:https?|ftp)://)
            |
            www\.
            |
            (??:https?|ftp)://)www\.
         )

 

which means must start with www. or http:// or or http://www.

(http could also be https or ftp)

their are others ways of thing this but the above should work fine

Nah that didn't do it MadTechie. I think that that's because that part is the part that searches for an unlinked link.

 

Oh and this is what I'm talking about.

Say someone writes a URL like this in a box:

http://www.google.com

 

It doesn't become linked. I want to search for those links and link them, but if they are between the url tags then I don't want to link them.

 

Make sense?

Nope i'm still lost..

i took another look at effigy code and from what i can tell this is how it would work

 

[url=http://www.somewebsite.com]skip1[/url]
http://www.somewebsite.com <-- Find 1
[url=www.somewebsite.com]skip2[/url]
www.somewebsite.com <-- Find2
[url=http://somewebsite.com]skip3[/url]
http://somewebsite.com <-- Find 3

 

urls tagged in url tags are found and outsides are skipped, and your saying you have a problem matching "skip2" but skip1 works fine..

I don't see how skip1 would work with a negative lookbehind of "url"

Okay. Skip1 is not working. It replaces skip1 when it shouldn't. Skip2 works and skip 3 works. It should also not replace tags like this:

[url]http://www.somewebsite.com[/url]

And all the other combination's of that.

 

Skip1 works only when there are NO SPACES with the text inside. So:

 

[url=http://www.somewebsite.com]this text has space and it will find it and make it an active link[/url]

[url=http://www.somewebsite.com]nospace[/url] < this one will work properly.

[url=www.somewebsite.com]this text has space but this one will be skipped[/url]

 

Making more sense?

 

Thanks for the help as well. I appreciate it.

That's tricky. Since the start is triggered by two possible patterns, the lookbehind assertion will pass for the second if it is preceded by the first. A better approach might be the following:

[tt]

preg_match_all('%

(

(?>

### Protocol or start.

(?:

(?:(?:https?|ftp)://)

|

www\.

)

### Body: gobble everything except [/url]

(?:(?!\[/url\]).)+

### Avoid ending punctuation.

(?<!\p{P})

)

### Not followed by an url end.

(?!\[/url\])

)

  %x', $str, $matches);

Don't mind me.. just eaves-dropping.. if I understand correctly, you want to strip out all the spaces in the words between the opening and closing url tags, is that it?

If so, would this not work?

 

$str = '[url=http://www.website.com]Some text here with space with many spaces it seems[/url]';
preg_match('#(?<=\]).*(?=\[)#', $str, $match);
$match[0] = str_replace(' ', '', $match[0]);
$str = preg_replace('#(?<=\]).*(?=\[)#', $match[0], $str);
echo $str;

 

If I misunderstood, perhaps displaying a simple example of a before string and an after string would help me out better..

 

Cheers,

 

NRG

if the above exmaple is right, I suppose I could have just done this instead:

 

$str = '[url=http://www.website.com]Some text here with space with many spaces it seems[/url]';
$str = str_replace(' ', '', $str);

 

::)

But perhaps I'm missing something here?

 

Cheers,

 

NRG

Forgive me.. I appear to be brain farting all over the place today..

So after re-reading your post.. I think what you are saying is that the removal of spaces in the characters between the url tags are conditional on whether there is the 'http://' in the opening url tag or not, correct?

 

If so, why not simply do an if statement in conjunction with my first solution?

 

$str = '[url=http://www.website.com]Some text here with space with many spaces it seems[/url]';
if(preg_match('#http://#', $str)){ // remove spaces from text inbetween url tags
   preg_match('#(?<=\]).*(?=\[)#', $str, $match);
   $match[0] = str_replace(' ', '', $match[0]);
   $str = preg_replace('#(?<=\]).*(?=\[)#', $match[0], $str);
}
echo $str;

 

You can test this by removing the 'http://' part in $str and you will see the spaces in the text between the url tags remain.

Cheers,

 

NRG

Cheer's effigy, it works fine. (At the moment). Thanks so much.

 

@nrg_alpha: Thanks for posting. I didn't want to remove the spaces. I wanted a pattern that would find text that is a url, but isn't linked or surrounded in the url bbcode tags. Thanks though.

 

*solved*

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.