Log in

View Full Version : Help with preg_replace



qwikad.com
08-26-2014, 02:29 PM
I have this thing that inserts spaces around a link. So if an original link looks like this ++++http://qwikad.com++++ the end result will look like this ++++ http://qwikad.com ++++




$text = preg_replace( '/(\b)(http|ftp|https|mailto|HTTP|FTP|HTTPS|MAILTO)([\:\/\/])([A-z0-9~!@$%&*()_+:?,.\/;=#-]{2,}[A-z0-9~@$%&*_+\/=])/', " $2$3$4 ", $text);



Somehow it works with all the characters except the asterisk. With asterisks the end result looks like this: **** http://qwikad.com**** It doesn't put the space at the end of a URL. Can anyone tell me why it doesn't?

jscheuer1
08-26-2014, 03:31 PM
I haven't really looked closely at the code, but the * is a special character that means any or none of the character that precedes it. It may have to be specifically included and when doing so, it may have to be escaped one or more times to have it be recognized as itself (as an actual asterisk).

qwikad.com
08-26-2014, 04:20 PM
I see. Didn't think of it. Maybe I should first convert it to its HTML symbol &#42 ; and then use a &#42 ; instead of a * in that preg_replace. It might work... I think.

james438
08-26-2014, 04:26 PM
$text = preg_replace( '/(\b)(http|ftp|https|mailto|HTTP|FTP|HTTPS|MAILTO)([\:\/\/])([A-z0-9~!@$%&*()_+:?,.\/;=#-]{2,}[A-z0-9~@$%&*_+\/=])/', " $2$3$4 ", $text);
The above highlighted code will match pretty much anything until the end of the string or until it reaches a space.

Your first example ++++http://qwikad.com++++ does not work either.

Matching email and website addresses is not easy and I know of nothing that works perfectly. How common is it that you or someone will use **** or ++++ to wrap around your addresses? Sites can end in so many different ways. What I usually use to recognize the end of a website is some sort of whitespace, punctuation followed by whitespace, or an ending tag like </a>.

A very minor note, but your ending dash within the second set of square brackets should be escaped since the only meta characters allowed within square brackets are -, ^, and \.

qwikad.com
08-27-2014, 01:20 AM
Your first example ++++http://qwikad.com++++ does not work either.


You're right. I was just assuming that it was working with the + sign since I only saw it not working with the * sign. Just yesterday someone posted a paid ad and she had the ****link.com**** thing going around her link. I had to manually edit it. It doesn't happen often, but it does happen. Thanks for the help. Maybe I should just do something as silly as this, so that I'd never have to edit those type of ads again:




$text = preg_replace( '/.com*/', ".com *", $text);

$text = preg_replace( '/.com**/', ".com **", $text);

etc...

jscheuer1
08-27-2014, 02:22 AM
Interestingly, the + sign is another one of those special characters. It means 1 or more occurrences of the previous character. Once again, if it's to be used in a regular expression as itself it needs to be specifically indicated and probably escaped 1 or more times. Otherwise it might be interpreted only as having its special meaning.

james438
08-28-2014, 03:41 AM
True, + is another of those special characters and there are several to keep track of when using regular expressions, however, within square brackets, also known as character classes, many special characters are taken literally [!@#$%&*] will match those characters, but ^ at the beginning of a character class negates the character class so that [^!@#$%&*] will match everything except what is in the character class.

Still, escaping + or * won't hurt either. The only special characters in a character class are \, ^, and -. ref (http://us.php.net/manual/en/regexp.reference.character-classes.php)

qwikad.com, if you want to go the route of replacing .com** with .com ** I recommend using str_replace instead as it is meant for simpler replacements like that. php.net recommends using str_replace instead of preg_replace whenever you do not need to use preg_replace. ref (http://php.net/manual/en/function.str-replace.php) php.net does not go into detail why though and I rather wish it did. The reason I often hear is that php's regular expression engine is good, but rather processor heavy whereas Perl's is regarded as the most efficient.

Bionic
09-02-2014, 05:11 PM
http://online-code-generator.com/preg_match.php
http://www.phpliveregex.com/
http://txt2re.com/