Jump to content


Photo

Cutting letters out of a string!


  • Please log in to reply
3 replies to this topic

#1 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 01 August 2006 - 02:19 AM

how i cannot find a function! i want to cut these letters out then put them into another string in the order the were cut!

EDIT: by "these" i mean like an array of letters

or how to find all letters that were changed by str_replace...
"Syntax error" .. WHERE THE **** IS IT?!

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 01 August 2006 - 02:33 AM

Your not being very clear. Maybe your looking for substr(), but really... who knows.

#3 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 01 August 2006 - 02:38 AM

k i have an array or letters..(lets say $letters) and i have a sentence (say $text) I want to have it go through the sentance and find all the letters in the $letter array and remove them from the sentance. Then make them into there own array(called $removedletters)
"Syntax error" .. WHERE THE **** IS IT?!

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 01 August 2006 - 02:50 AM

Probably better ways around, but....

<?php

  $letters = array('a','b','c','d');
  $text = "this is a big bad string";
  foreach($letters as $letter) {
    if (strpos($text,$letter)) {
      $removedletters[] = $letter;
      $text = str_replace($letter,"",$text);
    }
  }

  echo "the letters ".implode(" ",$removedletters)." were removed from '$text'";

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users