PHP string function problem

Associate
Joined
24 Aug 2003
Posts
1,486
Location
Derbyshire
I have a series of (different) strings, I wish to perform an insert/replace (replace \n (newline characters with some text) on each of the strings but only between certain points/indices in them.

I'm currently carrying out a horrible mess of searching, sub string copying, preg'ing and reinsterting.

Can anyone suggest a better way - I've been trying to find a single function to do it - basically a "preg_replace" but only between certain points.
 
Split it, then replace text only on the indice you want, then implode:
Code:
<?php

$parts = preg_split('/pattern/', $string); // or can use explode

$parts[1] = str_replace("\n", '', $parts[1]);

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

?>
 
Cheers :)

I got into work this morning to find they changed the spec of what they wanted, :rolleyes: in the end I did something similar to what you suggested, just a little less elegantly due to some extra string trimming and extra concatenation I had to carry out.

thanks again
 
Dj_Jestar said:
the indice you want

Index*
shobon.png
 
Back
Top Bottom