View Full Version : creating an array of a string but changing & for and and visa versa?

09-02-2009, 01:09 AM
Hi All,

ok i have a wierd request :)

i am wondering if it is possible to create an array from a string where but each array value is a new possibility for that phrase.

for example if the phrase was
i like php and javascript

could i some how add that line to an array and all other possibe ways of writing that line

array[1] => i like php and javascript
array[2] => i like php & javascript

im sure there are other possibilities?

but i suppose what im looking for is a string replace function? which then adds the new value to an array?

is this possible
many thanks

09-02-2009, 01:53 AM

function andToAmpersand($item)
return str_replace(' and ', ' & ', $item);

$aPhrases = array(
'I like PHP and Javascript',

$aSecondPhrases = array_map('andToAmpersand', $aPhrases);
$aComplete = array_merge($aPhrases, $aSecondPhrases);

That what you're looking for?

09-02-2009, 02:16 PM
wow that is great thanks mate,

im having a brain fart at the mo and my mind has gone blank :)
are there any other symbols which are commonly used to abbreviate text, which i can do the same for?

thanks for your help mate...

09-02-2009, 08:24 PM
Actually, it says in the str_replace api that these parameters can all be an array. So you could write one array of $search, one array of $replace, and take an array for its $subject. This should then return an array as well, so you won't need to use an array_map at all. You can just use the str_replace, than merge it with the original.

09-02-2009, 08:43 PM
ah right cool, i will look into it. thanks


09-02-2009, 08:43 PM
...are there any other symbols which are commonly used to abbreviate text...?

None come to mind for commonly used abbreviations, but one rarely used may be "=" for "is", or something along those lines.

~ Mo

09-02-2009, 09:30 PM
cheers Mo, i couldn't think of any more commonly used one either, maybe there arnt any others?