Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Location
    Mentor, OH
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    I have looked everywhere and still I can't correctly code "preg_replace"

    Here's my problem: I can't correctly code for "preg_replace"; on correct input, I want $smiley_star & $check_mark to replace $smiley_left_arrow. The first code illustration works on incorrect input.

    INCORRECT INPUT Work fine:

    PHP Code:
    if (strlen($fname) <2)
    {
    die(
    "<p align='center'><i class ='vld'>Please enter a valid first name</i></p><img src='images/SmileyArrowLeft.png' width='200' height='200'>");  
    }

    else if (
    strlen($fname) >30)
    {    
    die(
    "<p align='center'><i class=\"vld\">Please enter a valid first name</i></p><img src='images/SmileyArrowLeft.png' width='200' height='200'>"); 
    ____________________________________________________________________________________________________ ________
    CORRECT INPUT gives Error Message: Warning: preg_replace() expects at least 3 parameters, 1 given in Warning: preg_replace() expects at least 3 parameters, 1 given in C:\wamp\www.........................\reservations2.php on line 52

    PHP Code:
    $smiley_left_arrow = ("img src='images/SmileyArrowLeft.png' width='200' height='200'>");

    $smiley_star = ("<img src='images/Correct.png'>");

    $check_mark=("<img src='images/CheckMark.png' width='17px' height='17px'>");

    // $ check_mark . $miley_star for correct input
    if ( (strlen($fname)>1) && (strlen($fname)<31) ) {
        
    preg_replace  ("$smiley_left_arrow, $smiley_star, $check_mark");
    echo (
    $check_mark $smiley_star); 
    I'm getting Error Message: Warning: preg_replace() expects at least 3 parameters, 1 given in Warning: preg_replace() expects at least 3 parameters, 1 given in C:\wamp\www.........................\reservations2.php on line 52

    Thanks in Advance,
    Lucien

  • #2
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    The manual is the best place to look for information regarding these funcitons. PHP: preg_replace - Manual

    Also, why are you passing one string into the preg_replace? It should be something like:

    PHP Code:
    preg_replace  ($smiley_left_arrow$smiley_star$check_mark); 
    Not only that, with the variables assigned to those variables you won't have much success.

    I suggest you read it's documentation and look through the sample code as well as learn a bit more about regular expressions and their purpose.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    The function preg_replace is meant for regular expression search and replace. Regular expressions are NOT standard strings. A regular expression should define a pattern for the string, giving it rules to follow. Regular expressions tell a string what it can and cannot be. Although you can set it statically, you must properly escape the characters that regex uses.

    On top of that, if you want to use a string like you are without putting all the backslashes in it to escape the characters, you need to use preg_quote() to escape each one (for the pattern).

    Results:
    PHP Code:
    $smiley_left_arrow "img src='images/SmileyArrowLeft.png' width='200' height='200'>";
    $smiley_star "<img src='images/Correct.png'>";
    $check_mark "<img src='images/CheckMark.png' width='17px' height='17px'>";

    // $check_mark . $smiley_star for correct input
    if((strlen($fname) > 1) && (strlen($fname) < 31))
    {
        
    $input $fname//I'm assuming is what contains the data to be replaced. If not, set it here
        
    $output preg_replace(preg_quote($smiley_left_arrow"\\"), $smiley_star.$check_mark$input);
        echo 
    $input;

    Edit:
    Apparently I can't put a backslash in single quotes so I had to double backslash in double quotes (to escape itself). Doing a single quote backslash will work just as well (secont argument of preg_quote).


    Edit:
    You need to place forward slashes on either side of the preg quote (on the outside of it) as well.
    Ex:
    PHP Code:
    '/'.preg_quote($smiley_left_arrow"\\").'/' 
    Last edited by Dubz; 04-29-2014 at 09:37 PM.


  •  

    Posting Permissions

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