Jump to content

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


phpQuestioner

Recommended Posts

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..............?

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

<?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 :)

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.