Jump to content

[SOLVED] Replace Help


Recommended Posts

And it'll work for any set of letters, by the way, if I change it up a bit.  Here:

 

$str = "There are some letters here!";

$letters = range("a-z");

$numbers = range("1-26");

$str = str_split("", $str);

array_walk($str, 'strtolower'):

foreach ($str as $k=>$v) {

    $pos = array_search($v, $letters);

    $newarr[] = $numbers[$pos];

}

$newstr = implode("", $newarr);

echo $newstr;

   

Link to comment
Share on other sites

function remove_nonalpha($str) {

  $let = range('a-z');

  $str = strtolower($str);

  if (!in_array($str, $let)) {

    return NULL;

  }

  return $str;

}

$str = "There are some letters here!";

 

$str = str_replace($bad, "", $str);

$letters = range("a-z");

$numbers = range("1-26");

$str = str_split("", $str);

array_walk($str, 'remove_nonalpha'):

foreach ($str as $k=>$v) {

    $pos = array_search($v, $letters);

    $newarr[] = $numbers[$pos];

}

$newstr = implode("", $newarr);

echo $newstr;

 

 

Fixed it to remove non-letter characters.

Link to comment
Share on other sites

Rewritten a 3rd time to let any numbers you currently have in there to stay AND to not recreate a $let array every time (saves resources).  You can remove the number thing if you want.

function remove_nonalpha($str, $key, $let = "") {
   if (!isset($let)) {
       $let = range("a-z");
   }
   $str = strtolower($str);
   if (!in_array($str, $let) || !is_numeric($str)) {
     return NULL;
   }
   return $str;
}
$str = "There are some letters here!";

$str = str_replace($bad, "", $str);
$letters = range("a-z");
$numbers = range("1-26");
$str = str_split("", $str);
array_walk($str, 'remove_nonalpha', $letters):
foreach ($str as $k=>$v) {
    $pos = array_search($v, $letters);
    $newarr[] = $numbers[$pos];
}
$newstr = implode("", $newarr);
echo $newstr; 

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.