Search code examples
phparrayspreg-split

Print_r is not working with preg_split


I writing a function to search within my database. I wanted to eliminate uneccessary white spaces between words so I used preg_split. I have not used preg split before so I used print_r to see if it did what I was hoping for it to do (elimminate white spaces). Yet when I used print_r I got nothing back on my screen to show the array after I typed words in the search.

function search_results($keywords){
    $returned_results = array();
    $where = "";

    $keywords = preg_split('/[\s]+/', $keywords);
    print_r($keywords);

}

I would greatly appreciate it if someone could give me an idea as to what I did wrong, or better tips.


Solution

  • Tyr this

    $base_str = "this    is a   string  with multiple    spaces between  some words.            "; 
    
    
    $start = microtime(1); 
    for ($i=0; $i<100000; $i++){ 
        $string = $base_str; 
        while(strpos($string, '  ') !== false) 
        { 
             $string = str_replace('  ', ' ', $string); 
        } 
    } 
    
    echo $string;