Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Preg_replace and wildcards


    I cannot explain my frustration with this, because I cannot find any tutorial or any explanation anywhere after multiple searches on Google to help me figure out my problem.

    EDIT: Omg...the frustration. I can't post my example because of the rules here. >_>"

    So, I'm trying to replace [url=*] with <a href="*">, however, I notice str_replace, what I was using at first, does not use wildcards. I see that preg_replace does, however, using "regular expressions" which I do not know much about. Every post I see on every search page just throws out a random answer to the author's problems, not explaining exactly what each character represents in their answer.

    So, as my first post, can you guys help me with this? I just simply want any link between [url= and ] to be changed to <a href=" and ">

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    From what you've posted, I'd say just go through your files and change [url=*] to what you want. Maybe I'm overlooking it, but I don't see what you are doing or trying to really accomplish. :-/

    Perhaps some examples?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    I posted an example and this forum thought I was posting a link, and it doesn't allow new users to post links. -_-

    And after typing a huge example, once again replacing the tags, my post is STILL prevent since "I'm posting links and am not allowed to do so" God I'm about to scream. This is -very- frustrating.

    I want to make a typical BBC link code be changed to a typical HTML link code. Not too difficult I believe.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,066
    Rep Power
    9398
    You can post links as long as they don't look like links. Like add a space or two. I think simply "example.com/path" (no http://) is enough.


    The generic way of doing BBCode tags looks like
    PHP Code:
    $after preg_replace('#\[url=(.*?)\](.*?)\[/url\]#i''<a href="$1">$2</a>'$before); 
    but that's actually unsafe. However the safer version is quite a bit more complicated. What is this for? Users on your site? Do you know who your users are? Do you trust them? If there's even the slightest doubt then you should go with the safer version (which I can certainly write out if you want).
  8. #5
  9. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    I notice that requinix (a greater authority!) has answered your question, but I'll continue to post the following as the explanation might be helpful.

    i am unable to test the following in PHP at the moment, but it should point you in the right direction:

    Code:
    $old_string = "hello [url=http://something.com] you";
    $pattern = "/\[url\=(.+)\]/gi";
    $replacement = '<a href="$1">';
    
    $new_string = preg_replace($pattern, $replacement, $old_string);
    In the pattern many characters have to be escaped, such as \[ and \=, using a back-slash. This is because they have a special meaning within regular expressions.
    The regex itself occurs between / and /. The following gi are called modifiers. g means a global replace (replace every occurrence) i means case-insensitive.
    ( and ) in the regular expression denote a capture group. What is caught between them can be referred to in the replacement by a number, $1, $2, etc. $0, by default, refers to the entire captured content.
    .+ Means "at least one character". .* would mean "any sequence of characters" and should be avoided where possible. Added: .* is used in requinix expression, but in a slightly modified form - ?.

    I've only tested it in a regex-app, not in PHP.
    Last edited by AndrewSW; November 20th, 2012 at 05:53 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Well before we go on with a more detailed version of the script you just typed up, could you explain to me exactly how this works? Seeing a bunch of characters like that just looks like...random jumbled up characters to me. I've never had to use such an elaborate code before with nothing but special characters in the line, if you know what I mean.

    And it's for my users on my site project I'm building.

    EDIT: I just noticed you posted Andrew. Thank you for the explanation; that helps a little bit. Just seeing a bunch of /\ and .-? + * is a bit confusing without an explanation.

    so, lets say I have three words. Two of them are set-in-stone, so to speak, where as the third word would be a wildcard.

    IE: one * two

    Where * is the wildcard, of course. How would this be put in the string?

    Sorry if that's a little confusing; I learn best by seeing several different examples using the same function, so I can compare and whatnot.

    Thanks~
  12. #7
  13. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    What about the BBCode extension?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    I briefly looked into this beforehand, however I do not want to any extensions for now. I want to see if I can accomplish this without. Not to mention my code strips all tags from the text a user submits with strip_tags, then replaces specific characters in sequence with HTML tags after the previous tags are stripped.
  16. #9
  17. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    I am not the best person to ask and someone will probably correct me:

    PHP Code:
    $old_string 'one something two';
    $pattern '/one (.*?) two/gi';
    $replacement 'one else two'
    In some variants of regex we would need to use \s{1} to represent a single space (well, whitespace) character.

    Or possibly

    PHP Code:
    $pattern '/one\b(.*?)\btwo/gi'
    where \b means any word separator, such as a space.

    The question mark in .*? means "be lazy": capture as few characters as necessary to meet the pattern.

    I will not object to being corrected
  18. #10
  19. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Here is a regex designer you can use to test expressions, but it is not specific to PHP. I don't know if there is a more suitable, PHP-specific alternative available somewhere..?
    Edited: actually, this designer is for Windows.

    [There is a reasonable amount of consistency across different languages, but they all have their quirks, inconsistencies!]

    This online tool looks reasonable.
    Last edited by AndrewSW; November 20th, 2012 at 06:26 PM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Wow, thank you so much for all that. You've put quite an effort to help me out here, I appreciate that.

    Ok, so if I had a script that would put a word between two [ and ], I would have something like:

    $old_string = '[ something ]';
    $pattern = '/[ (.*?) ]/';
    $replacement = '[ else ]';

    Is this correct? Just to put something in brackets?
  22. #12
  23. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    You need to escape the square brackets with back-slashes, otherwise they have a special meaning to the regex parser:

    Code:
    $pattern = '/\[ (.*?) \]/';
    http://www.regular-expressions.info/
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Oh, I see. Just like a lot of other PHP code, such as echo and print where you put a \ in front of the ' or " to make sure the statement doesn't end if you're using that in your echo or print command.

    And $1 outputs what was found?
  26. #14
  27. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    And $1 outputs what was found?
    $1 outputs what was first explicitly captured between ( brackets ), $0 would be the entire captured expression.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Awesome thank you very much~ I'll test this out in a bit and reply back with the results.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo