JasonLewis Posted August 2, 2008 Share Posted August 2, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/ Share on other sites More sharing options...
MadTechie Posted August 3, 2008 Share Posted August 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/#findComment-606883 Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Author Share Posted August 4, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/#findComment-607042 Share on other sites More sharing options...
MadTechie Posted August 4, 2008 Share Posted August 4, 2008 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" Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/#findComment-607326 Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Author Share Posted August 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/#findComment-607330 Share on other sites More sharing options...
effigy Posted August 4, 2008 Share Posted August 4, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/#findComment-607605 Share on other sites More sharing options...
nrg_alpha Posted August 4, 2008 Share Posted August 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/#findComment-607785 Share on other sites More sharing options...
nrg_alpha Posted August 4, 2008 Share Posted August 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/#findComment-607789 Share on other sites More sharing options...
nrg_alpha Posted August 4, 2008 Share Posted August 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/#findComment-607811 Share on other sites More sharing options...
JasonLewis Posted August 5, 2008 Author Share Posted August 5, 2008 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* Quote Link to comment https://forums.phpfreaks.com/topic/117789-solved-the-url-replace-code-is-having-trouble/#findComment-608378 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.