Jump to content

[SOLVED] how to select only a specifc amount of words from a variable


phpQuestioner

Recommended Posts

you should say like..

how do you know they are words? do they have delimiters or maybe size related to split the words?

 

I dont think php knows if they are words or not.  :o

 

 

or u mean http://us2.php.net/language.variables.variable  ?

not exactly what I meant; I know that I can use an explode, but I do not want to explode this variable until I get to a specific place in the variable. I would like to display the first 3 words in the variable and then display 3 more words from this variable in a different area of my page.

 

like this:

<?php

echo "<table><td>";

$myvar="this is a string that I want to dissect ";
$seperate = explode(" ", $myvar, 3);
for($i = 0; $i < count($seperate); $i++){
echo "<li>$seperate[$i]</li>";
}

echo "</td><td>";

// then some where else in my page pull 3 more addition words from this variable

$myvar="this is a string that i want to dissect ";
$seperate = explode(" ", $myvar, 3);
for($i = 0; $i < count($seperate); $i++){

// only this time I want to echo the next 3 words from this variable here and exclude the 3 words above that I have already pulled out from this variable

}

echo "</td></table>";

?>

 

 

you see what I am saying..............?

Why can't you just:

 

<?php

echo "<table><td>";

$myvar="this is a string that i want to dissect ";
$seperate = explode(" ", $myvar);
echo "<li>$seperate[0]</li><li>$seperate[1]</li><li>$seperate[2]</li>";


echo "</td><td>";

// then some where else in my page pull 3 more addition words from this variable


// only this time I want to echo the next 3 words from this variable here
echo "<li>$seperate[3]</li><li>$seperate[4]</li><li>$seperate[5]</li>";

}

echo "</td></table>";

?>

<?php

echo "<table><td>";

$myvar="this is a string that i want to dissect ";
$seperate = explode(" ", $myvar);
for($i = 0; $i < count($seperate); $i++){
echo "<li>$seperate[$i]</li><li>$seperate[$i+1]</li><li>$seperate[$i+2]</li>";
echo "</td><td>";
}


echo "</td></table>";

?>

Not quite understanding you there phpQuestioner...

 

But I did forget to take out an extra }

<?php

echo "<table><td>";

$myvar="this is a string that i want to dissect ";
$seperate = explode(" ", $myvar);
echo "<li>$seperate[0]</li><li>$seperate[1]</li><li>$seperate[2]</li>";


echo "</td><td>";

// then some where else in my page pull 3 more addition words from this variable


// only this time I want to echo the next 3 words from this variable here
echo "<li>$seperate[3]</li><li>$seperate[4]</li><li>$seperate[5]</li>";



echo "</td></table>";

?>

<?php

echo "<table><td>";

$myvar="this is a string that i want to dissect ";
$seperate = explode(" ", $myvar);
for($i = 0; $i < count($seperate); $i++){
echo "<li>".$seperate[$i]."</li><li>".$seperate[$i+1]."</li><li>".$seperate[$i+2]."</li>";
echo "</td><td>";
}


echo "</td></table>";

?>

 

Fixed :)

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.