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
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.
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):
1) $variable = "I have a ball"; str_replace(' a ', ' ',$variable); Code (markup): 2) $variable = "Nice car"; preg_split('/ +/',$variable); Code (markup):
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:
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 ??
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
<?php ... $filtedstring = preg_replace('/\s*\bWILL\b\s*/i', ' ', $filtedstring); $filtedstring = str_replace('_', ' ', $filtedstring); [b]trim($filtedstring);[/b] $split = preg_split('/ +/',$filtedstring); ... ?> Code (markup):