taking part of a string?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluewalrus
Forum Newbie
Posts: 3
Joined: Tue Oct 07, 2008 1:03 am

taking part of a string?

Post by bluewalrus »

I found this could but dont really understand it. Will this search a string and then put part of it in a variable? From what your searching for till another part or does anyone know of a way to do that?... Say this was a string "hello 5there php5 people". If i wanted to search from the first instance of 5 to the second. Thanks for your time and any ideas.

Code: Select all

<?php
function find_first_of($haystack, $needlesAsString, $offset=0)
{
  $max = strlen($needlesAsString);
  $index = strlen($haystack)+1;
  for($ii=0; $ii<$max;$ii++){
    $result = strpos($haystack,$needlesAsString[$ii], $offset);
    if( $result !== FALSE  && $result < $index)
      $index = $result;
  }
  return ( $index > strlen($haystack)? FALSE: $index);
}
?>
 
Example:
<?php
$test="Ralph: One of these days, Alice!!";
$look_for=":!,";   // punctuation marks
 
$ss = 0;
while( $answer=find_first_of($test,$look_for,$ss) ) {
  echo $answer . "\n";
  $ss = $answer+1;
 }
?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: taking part of a string?

Post by requinix »

That function gives you the earliest offset of a character found in $needleAsString.
If the needle is :!, then it'll find the location of the next colon, exclamation mark, and comma, and return the earliest one.

Which kinda helps you with your problem, but there's a much simpler way.

Code: Select all

<?php
 
$string = "hello 5there php5 people";
$find = "5";
 
$first = strtok($string, $find); // "hello "
$second = strtok($find); // "there php"
$third = strtok($find); // " people"
$fourth = strtok($find); // false
 
?>
bluewalrus
Forum Newbie
Posts: 3
Joined: Tue Oct 07, 2008 1:03 am

Re: taking part of a string?

Post by bluewalrus »

so do i need to go threw all of them like that? or could i run a foreach?
I'm trying to use it in this code to add an id under each image all of the ids are in one text file seperated by the word id at each one.

Code: Select all

<?php
  //getting all files of desired extension from the dir using explode
  $dirname = "uploads/";
  $dir = opendir($dirname);
  $read =" uploads";
  $fh = fopen($read, 'a+');
 
  while(false != ($file = readdir($dir)))
  {
    if(($file != ".") and ($file != ".."))
    {
      $fileChunks = explode(".", $file);
      if($fileChunks[1] == 'jpg' || 'jpeg' || 'gif' || 'png') //interested in second chunk only
      {      
//list($width, $height, $type, $attr) = getimagesize("uploads/" . $file);
  //    if ($height > 600) {
    //  } 
    echo '<a class="thumbnail" href="#thumb"><img src="uploads/' . $file . '" alt="esef user image" border="0" /><span><img src="uploads/' . $file . '" alt="esef user image" border="0" /><br /></span></a>';
  }
    }
  }
  closedir($dir);
?>
Post Reply