phpOk so its time for some more PHP tips.

Stripping characters from a string
If you want to strip certain characters from a string you can do so like this.
$string='I-need-to-get-rid-of-the-dashes'; $string=str_replace("-"," ",$string); //result is - I need to get rid of the dashes

Here is another tid-bit. Splitting text. If you want to split a paragraph, you can use the following!

$string='This sentence has many words but i want to restrict it to only five.'; $string=implode(" ", array_slice(preg_split("/\s+/", $string), 0, 5)); //result is - This sentence has many words

You can also go a little further and split a paragraph or sentence in the middle. Missing the beginning and end and only using the middle part. See below.

$string='This sentence has many words but i want to restrict it to only five.'; $string=implode(" ", array_slice(preg_split("/\s+/", $string), 3, 5)); //result is - many words but i want

Easy enough! Just a little but they’re gems ;)

Leave a Reply

You must be logged in to post a comment.