Jump to content

How do I str_replace this?


Jeffro

Recommended Posts

I sometimes have words thrown into my database with the following symbol: 

…

 

What I'm trying to achieve is this..

Anytime that symbol is found, replace it and all previous letters..  back to the first space encountered... with nothing. 

 

Make sense? 

If I had  the sentence

"the lazy brown d..…

, it should return for me "the lazy brown" 

 

If I was just replacing the weird code, it would be a simple str_replace, but not sure how to make the letters before the symbol (up to the first space) go away.

 

Ideas? 

Link to comment
https://forums.phpfreaks.com/topic/236048-how-do-i-str_replace-this/
Share on other sites

<?php

$search_chars = array();
for($i=128;$i<=255;$i++){array_push($search_chars,$i);}

$string = "the lazy brown d..…";
$orig = $string;

function strpos_chararray($haystack, $needles)
{
  if(is_array($needles))
  {
    foreach($needles as $key => $str)
{
  if(is_numeric($str)){$str = chr($str);}
  if(is_array($str))
  {
        $pos = strpos_array($haystack, $str);
      }
  else
  {
        $pos = strpos($haystack, $str);
  }
      if ($pos !== FALSE)
  {
        return $pos;
      }
    }
if ($pos === FALSE)
{
      return FALSE;
    }
  }
  else
  {
    return strpos($haystack, $needles);
  }
}
function rebuild($string)
{
  global $search_chars;
  $str = explode(' ',$string);
  foreach($str as $str_bit)
  {
$chk = strpos_chararray($str_bit, $search_chars);
if($chk === FALSE)
{
  $new_str .= ' '.$str_bit;
}
else{break;}
  }
  return $new_str;
}

if(strpos_chararray($string, $search_chars))
{
  $string = rebuild($string);
}

echo "ORIGINAL >> ".$orig."<br>";
echo "NEW >> ".$string."<br>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.