Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CMS - Insert a string in a specific part

Status
Not open for further replies.

rockstardub

Programmer
Dec 29, 2004
42
0
0
US
What I want: I want the user to be able to write their text, and when the basic text is requested, it will scatter it with images. I don't want users to write HTML tags like <img src>.

I want the php to count 20 words into the string, then insert an html image tag. I don't want to cut a word in half.

I don't know what to do,
1. if I turn it into an array to enter the string by splicing the array then it takes away formatting.
2. if I str_word_count() then I get the location I need, but have no way of manipulating it.

I think the best way to do it would be to make a loop that counts spaces, something like this...

<?

$text = 'a lot of text here';
$string = '<img src="something.jpg">';

for($i = 0; $i < spaces in the string) {
$i++;
if ($i >= 20) {
insert $string into $text here;
}
}

?>
 
It's not an issue of removing, it's an issue of adding.

example.jpg
 
I am unclear of what you are trying to accomplish.
Are you trying to split the string at the 20th character, add the image string to the end of the first part, then reconnect the two string back together again?
 
Ya, that sounds right.
(I want it to count spaces to know where to insert)

So something like this:
<p>Why doesn't this have a solution, why does it have to be so difficult?</p>

Turned into this:
<p>Why doesn't this have a solution, why does it have <img src="" align="left">to be so difficult?</p>
 
How about this?
Code:
$str = "<p>Why doesn't this have a solution, why does it have to be so difficult?</p>";
$expl = explode($str, " ");
$inc = 20;
$img = '<img src="" align="left">';

for ($i=0; $i<count($expl); $i+=$inc)
{
  $expl[$i] .= $img;
}

$ret = implode($expl, " ");
echo $ret;

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Chessbot, got an error when I tried your script, but thanks for throwing in the explode reminder. I got it to work, and here's how...

<?

//In this example I will use the 3rd & 5th char, because its a short string
$location[] = 3; $image[] = '<img src="#1">';
$location[] = 10; $image[] = '<img src="#2">';

$string = "<p>Why doesn't this have a solution,
why does it have to be so difficult?</p>";

$string = explode(' ', $string);

for ($i = 0; $i < count($string); $i++) {
array_splice($string, $location[$i], 0, $image[$i]);
}

$string = implode(' ', $string);

echo $string;

?>

This was a quality problem,
Thanks for the help Tek-Tippers...

- Rockstar dub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top