Re: Manipulating strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> I have a series of questions.

> How do I count the number of <br /> 's in a string?

> How do I add text in the middle of a string, let's say after the
> 3rd <br />?

If all you want to do is count the number of line breaks, then the
substr_count function that Micah recommends will do the job. I suspect
that you are interested in more than counting paragraphs, however. To
insert an ad as you describe, I would convert the string to an array,
edit the desired element, then reconstruct the string. It's much
easier than it sounds. You can also get the number of elements in the
array, if you desire:

<?PHP
//define string
$str="text_0<br />text_1<br />text_2<br />text_3<br />text_4<br
/>text_5";

//create array of string elements and (optionally) get the number of
elements
$para=explode("<br />",$str);
$elements=count($para)

//edit the desired element(s)
$para[3]="[AD] ".$para[3];

//reconstruct the string
$str=implode("<br />",$para);
?>
-- 
Crash
Committed to the search for intraterrestrial intelligence.





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux