SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace() with /i modifier

    use eregi_replace() or preg_replace() with the /i modifier instead
    i dont understand how i can use this...

    can some explain how this would work if my variables are $search, $replace and $text?
    hmm...

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    The /i modifier stands for case insensitive. Capitalization and small letters are ignored.

    So searching for

    $var = "search";
    preg_match("/SeArCh/i", $var);

    would match.

    preg_match("/SEARCH/", $var);

    instead would not.

  3. #3
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so would preg_replace(/$search/i,$replace,$text) work?
    hmm...

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At a guess I would say so. Not absolutely sure, though.

  5. #5
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    does any body know how i could do it using $search as the search string, $replace as the replacement string and $text as the string being searched?
    hmm...

  6. #6
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This searches the variable text for the contents of the variable $search and replaces it with the contents of the variable $replace

    $text = preg_replace("/$search/i",$replace,$text);

    Or do you want to search for "$search" ie finding the dollarsign and then the word sign?

  7. #7
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i keep getting this error:

    Warning: Compilation failed: unmatched parentheses at offset 1 in /home/randompc/public_html/mumblingjoe/inc/functions.inc on line 109

    Warning: Compilation failed: missing ) at offset 2 in /home/randompc/public_html/mumblingjoe/inc/functions.inc on line 109


    code (109 is the preg_replace() line):

    PHP Code:
    $text stripslashes($text);
    $text str_replace("\n","<br>",$text);

    if (
    $smilies == "1") {

        
    $sql "SELECT * FROM smilies";
        
    $result mysql_query($sql);

        while (
    $row mysql_fetch_array($result)) {

            
    $search $row[emoticon];
            
    $replace $row[smilie];
            
    $replace "<img src = [url]http://mumblingjoe.randompc.com/img/smilies/[/url]$replace>";

            [
    b]$text preg_replace("/$search/i",$replace,$text);[/b]

        }


    hmm...

  8. #8
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are several characters that have a special meaning when using regexp.

    Some of them are \ ( ) [ ] . ? * + { } |

    Since you are searching for emoticons that look probably like this
    you have to escape them in the search string.

    So to search for "" the variable $search must be ":\)"

    You have to escape every character mentioned above with a "\" then it should work.

  9. #9
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To save you a bit trouble try this for the line:

    old: $search = $row[emoticon];

    new:
    PHP Code:
    $search preg_quote($row['emoticon']); 

  10. #10
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks alot

    ive finally got it to work properly

    i was just gonna ask you about the preg_quote thingy aswell hehe
    hmm...


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
  •