Results 1 to 8 of 8

Thread: Help with preg_replace

  1. #1
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Help with preg_replace

    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 ++++

    Code:
    $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?
    Last edited by jscheuer1; 08-26-2014 at 03:28 PM. Reason: format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default

    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.

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    $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 \.
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #5
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by james438 View Post
    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:

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

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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

    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 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.
    To choose the lesser of two evils is still to choose evil. My personal site

  8. #8
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

Similar Threads

  1. Resolved preg_replace
    By ggalan in forum PHP
    Replies: 2
    Last Post: 02-25-2012, 05:10 AM
  2. preg_replace
    By neo_philiac in forum PHP
    Replies: 0
    Last Post: 12-22-2008, 04:36 PM
  3. Resolved preg_replace confuses me.
    By MrEvil in forum PHP
    Replies: 8
    Last Post: 11-11-2008, 11:06 AM
  4. Can't use brackets in preg_replace
    By rctxtreme in forum PHP
    Replies: 1
    Last Post: 08-30-2007, 11:06 PM
  5. PCRE and preg_replace
    By james438 in forum PHP
    Replies: 2
    Last Post: 07-11-2007, 08:43 PM

Bookmarks

Posting Permissions

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