...

View Full Version : Replacing special letters/marks etc. from a string



Haaskis
10-24-2009, 11:51 AM
Heya,

So, I have this little problem - replacing special things (can't remember the correct word right now) from a string. This is to show links in more understandable way, example:

"this_file_is_awesome.php", translating to "this file is awesome".

This far I have gone around it with this little code:



$filec1 = str_replace(".php", "", $file1);
$filec2 = str_replace("axaxa", "", $filec1);
$filec3 = str_replace("oxoxo", "", $filec2);
$filec4 = str_replace("_", " ", $filec3);
//Show string without garbage
echo $filec4;


But Im afraid it is quite uneffective way, is there a better way to do this? Right now my current code doesn't even have half of what it should, and having 25 different valuables would be like, bad?

I dont want ready code, but someone showing direction is what I'm looking for ;).

oesxyl
10-24-2009, 01:26 PM
Heya,

So, I have this little problem - replacing special things (can't remember the correct word right now) from a string. This is to show links in more understandable way, example:

"this_file_is_awesome.php", translating to "this file is awesome".

This far I have gone around it with this little code:



$filec1 = str_replace(".php", "", $file1);
$filec2 = str_replace("axaxa", "", $filec1);
$filec3 = str_replace("oxoxo", "", $filec2);
$filec4 = str_replace("_", " ", $filec3);
//Show string without garbage
echo $filec4;


But Im afraid it is quite uneffective way, is there a better way to do this? Right now my current code doesn't even have half of what it should, and having 25 different valuables would be like, bad?

I dont want ready code, but someone showing direction is what I'm looking for ;).



$reppairs = array("/.php/" => "",
"/axaxa/" => "",
"/oxoxo/" => "",
"/_/" => " ");
$what = array_keys($reppairs);
$with = array_values($reppairs);
$result = preg_replace($what, $with, $initial);

order of pairs in $reppairs is important.

best regards

Haaskis
10-24-2009, 04:36 PM
Thank you, oesxyl.

I played and researched around a bit more with arrays and ended up doing this;


$result = $create;
$match = array(
"/ /",
"//",
"//",
"//",
"//",
);
$replace = array(
"_",
"axax",
"oxox",
"AXAX",
"OXOX",
);

$result = preg_replace($match, $replace, $result);$create is the original string I want to change.

Works perfectly for my purposes.

Lamped
10-25-2009, 10:28 PM
I'd like to be a little pedantic and make the following point:

From: http://php.net/manual/en/function.preg-match.php

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

Consider using str_replace or str_ireplace instead - you can still use the arrays like you did in the preg_replace.

http://uk2.php.net/manual/en/function.str-replace.php
http://uk2.php.net/manual/en/function.str-ireplace.php

oesxyl
10-25-2009, 10:44 PM
I'd like to be a little pedantic and make the following point:

From: http://php.net/manual/en/function.preg-match.php


Consider using str_replace or str_ireplace instead - you can still use the arrays like you did in the preg_replace.

http://uk2.php.net/manual/en/function.str-replace.php
http://uk2.php.net/manual/en/function.str-ireplace.php
and is better to write 7 or more str..., or a loop, then one preg_replace? :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum