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
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>";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.