str_replace & explode function problem

Discussion in 'PHP' started by dracula51, Jun 9, 2009.

  1. #1
    hello
    im facing a problem with str_replace() & exploade() function

    str_replace() problem

    for example, if i use this command
    $variable = "I have a ball";
    str_replace("a", "", $variable);


    the result will be I hve bll but i want: I have ball

    that means i want to replace exactly a (a, single word) not replace a from the other word like it did from have(hve) & ball(bll)


    explode() problem

    [COLOR="DarkRed"][B]$variable = "Nice     car";[/B][/COLOR] (five space betwen NICE & CAR)
    [B][COLOR="#8b0000"]explode (" ", $variable);[/COLOR][/B]
    Code (markup):
    so it'll make the array of $variable & the result is like this:
    $variable[0] will be "Nice"
    $variable[1] will be " " (a space)
    $variable[2] will be " " (a space)
    $variable[3] will be " " (a space)
    $variable[4] will be " " (a space)
    $variable[5] will be "car"


    so its seen that explode a single SPACE & keep other 4 spaces in array (as there were total 5 spaces)

    but i want it to remove all space & the result should be:
    $variable[0] will be "Nice"
    $variable[1] will be "car"


    see? only two array NICE & CAR




    plz help me
     
    dracula51, Jun 9, 2009 IP
  2. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    You're replacing everywhere there is an 'a' with a "" no character/space.
    try using space before and after like ' a ' .
    I've never tried that before but PHP considers white space a space so maybe?
    If it works it will stop it form taking it from anywhere else since those instances of 'a' will have characters before and after them.

    good luck, hope it works.
     
    ezprint2008, Jun 10, 2009 IP
  3. Sudoku-Master

    Sudoku-Master Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    first you have to eleminate multiple delimiter. for the example I use now the : as delimiter....
    
    $variable = "Nice:::::car"; (five space betwen NICE & CAR)
    while (preg_match("/::/", $variable)
    {
    $variable=str_replace("::",":",$variable);
    }
    explode (":", $variable);
    
    Code (markup):
     
    Sudoku-Master, Jun 10, 2009 IP
  4. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    1)
    
    $variable = "I have a ball";
    str_replace(' a ', ' ',$variable);
    
    Code (markup):
    2)
    
    $variable = "Nice     car"; 
    preg_split('/ +/',$variable);
    
    Code (markup):
     
    stOK, Jun 10, 2009 IP
  5. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Using str_replace isn't ideal. Consider the following example:-

    echo str_replace(' a ', ' ', 'I have a ball'); // I have ball
    echo str_replace(' a ', ' ', 'I have ball a'); // I have ball a
    echo str_replace(' a ', ' ', 'a I have ball'); // a I have ball
    PHP:
    This is better:-

    echo preg_replace('/\s*\ba\b\s*/', ' ', 'I have a ball'); // I have ball
    echo preg_replace('/\s*\ba\b\s*/', ' ', 'I have ball a'); // I have ball
    echo preg_replace('/\s*\ba\b\s*/', ' ', 'a I have ball'); // I have ball
    
    PHP:
     
    clarky_y2k3, Jun 10, 2009 IP
  6. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    tnx everybody

    tnx stOK. ur 2nd example is really useful

    tnx clarky_y2k3 for the preg_replace code. nice one. but i found a problem here. its case sensetive.

    echo preg_replace('/\s*\ba\b\s*/', ' ', 'I have a ball'); // I have ball
    echo preg_replace('/\s*\ba\b\s*/', ' ', 'I have A ball'); // I have A ball
    PHP:
    is there any solution ??
     
    dracula51, Jun 10, 2009 IP
  7. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #7
    Sure, change the regular expression to this:-

    /\s*\ba\b\s*/i
    PHP:
     
    clarky_y2k3, Jun 10, 2009 IP
  8. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    its working nice...tnx clarky_y2k3
     
    dracula51, Jun 10, 2009 IP
  9. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    here's the final PHP code but still facing problem :(


    <?
    
    $string = $_REQUEST['s'];
    
    // Filtering search string
    $filtedstring = preg_replace('/\s*\bABOUT\b\s*/i', ' ', $string);
    $filtedstring = preg_replace('/\s*\bAT\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bBY\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bFOR\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bIN\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bOF\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bOFF\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bON\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bTO\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bUP\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bWITH\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bA\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bAN\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bTHE\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bAND\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bBUT\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bDO\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bALL\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bALSO\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bAM\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bARE\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bAS\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bBE\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bIS\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bOR\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bSO\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bTOO\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bWAS\b\s*/i', ' ', $filtedstring);
    $filtedstring = preg_replace('/\s*\bWILL\b\s*/i', ' ', $filtedstring);
    $filtedstring = str_replace('_', ' ', $filtedstring);
    
    
    $split = preg_split('/ +/',$filtedstring);
    $like = "";
    
    if (count($split)>0)
      {
    	for($i=0;$i<count($split);$i++)
    	{
    	if ($i != '0')
    		{ $like.= "OR "; }
    	$like.= "tags LIKE '%$split[$i]%' OR title LIKE '%$split[$i]%' ";
    	}
      }
    
    echo $like;
    
    ?>
    PHP:
    so lets consider this link: http://my-domain/page.php?s=storm+lightening+thunder
    [B]result:[/B] tags LIKE '%storm%' OR title LIKE '%storm%' OR tags LIKE '%lightening%' OR title LIKE '%lightening%' OR tags LIKE '%thunder%' OR title LIKE '%thunder%'
    hmm its good result
    Code (markup):
    now this link: http://my-domain/page.php?s=storm+with+thunder
    [B]result: [/B]tags LIKE '%storm%' OR title LIKE '%storm%' OR tags LIKE '%thunder%' OR title LIKE '%thunder%'
    [B]note:[/B] [I][COLOR="Navy"]with[/COLOR][/I] is one of the [I]filtered word[/I]. so it is ignored in result. & look...this FILTERED WORD is in the middle. i mean its not at the start or at end
    Code (markup):
    here's the main problem begin
    now this link: http://my-domain/page.php?s=storm+thunder+with
    [B]result:[/B] tags LIKE '%storm%' OR title LIKE '%storm%' OR tags LIKE '%thunder%' OR title LIKE '%thunder%' [COLOR="Red"]OR tags LIKE '%%' OR title LIKE '%%'[/COLOR]
    Code (markup):
    & this link: http://my-domain/page.php?s=with+storm+thunder
    [B]result:[/B] [COLOR="Red"]tags LIKE '%%' OR title LIKE '%%' OR[/COLOR] tags LIKE '%storm%' OR title LIKE '%storm%' OR tags LIKE '%thunder%' OR title LIKE '%thunder%'
    Code (markup):
    see the red marked extra line ??? When any of those FILTERED WORD is at the begining or end of a string...there shows some extra code

    how can i fix this plz help
     
    dracula51, Jun 13, 2009 IP
  10. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #10
    <?php
    ...
    
    $filtedstring = preg_replace('/\s*\bWILL\b\s*/i', ' ', $filtedstring);
    $filtedstring = str_replace('_', ' ', $filtedstring);
    [b]trim($filtedstring);[/b]
    
    $split = preg_split('/ +/',$filtedstring);
    
    ...
    ?>
    Code (markup):
     
    stOK, Jun 14, 2009 IP
  11. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    tnx but u hav to type like this : $filtedstring = trim($filtedstring); :)
     
    dracula51, Jun 14, 2009 IP