homepage Welcome to WebmasterWorld Guest from 54.211.235.255
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How can I reduce this one line of code
str_replace
Roel




msg:3351716
 1:48 pm on May 28, 2007 (gmt 0)

Hi guys

How can I reduce this:

str_replace('/','',str_replace('-','',str_replace('&','',str_replace(')','',str_replace('(','',str_replace('"','',str_replace('.','',str_replace('!','',str_replace('?','',str_replace(' ',' ',str_replace(', ',', and ',str_replace(' ',', ',$title),-1)))))))))))

Thanks heaps

 

jatar_k




msg:3351719
 2:01 pm on May 28, 2007 (gmt 0)

you can use an array with str_replace

$badchars = array('/', '-', '&', ')', '(', '"', '.', '!', '?');
str_replace($badchars,'',$title);
str_replace(', ',', and ',$title);

that is much easier to read.

you seem to be replacing a space with a space in there, I skipped that. You also are replacing spaces with comma space, not too sure about that one either, I left it out.

Roel




msg:3351729
 2:20 pm on May 28, 2007 (gmt 0)

Thanks heaps Jatar!

It's actually become a bit longer :)

$title=str_replace(':','', str_replace(';','', str_replace('/','', str_replace('-','', str_replace('&','', str_replace(')','', str_replace('(','', str_replace('"','', str_replace('.','', str_replace('!','', str_replace('?','', str_replace(' ',' ', str_replace(' ',', ',trim($title))))))))))))))

I replace two spaces with one and space with comma space

Any ideas how I could use preg to do this?

[edited by: jatar_k at 2:38 pm (utc) on May 28, 2007]
[edit reason] fixed sidescroll [/edit]

Roel




msg:3351731
 2:25 pm on May 28, 2007 (gmt 0)

Or is there another way to remove all special chars and numbers from a string, leaving ONLY text (a-z, A-Z)?

jatar_k




msg:3351743
 2:37 pm on May 28, 2007 (gmt 0)

regular expressions

[php.net...]

I still don't get why you replace every space with comma space, but that's just me ;)

it is a bit longer but much more readable

are you cleaning user submitted data?

Roel




msg:3351766
 2:58 pm on May 28, 2007 (gmt 0)

Yeah I looked at preg_replace but it seems very complex. Could you get me started?

I am cleaning up a list of words and then I seperate them by comma.

CDNQuilter




msg:3351883
 5:58 pm on May 28, 2007 (gmt 0)

" Or is there another way to remove all special chars and numbers from a string, leaving ONLY text (a-z, A-Z)? "

Try:

$before ='This is a sentence )*(a(B*Z&abc%QER12356';

$after = preg_replace( '/[^a-zA-Z]/', '', $before);

echo $before.'<br />'.$after.'<br />';

to get

This is a sentence )*(a(B*Z&abc%QER12356
ThisisasentenceaBZabcQER

or add a space to the pattern to keep the spacing between the words

$after = preg_replace( '/[^a-zA-Z ]/', '', $before);

echo $before.'<br />'.$after.'<br />';

to get

This is a sentence )*(a(B*Z&abc%QER12356
This is a sentence aBZabcQER

PS
For what you are doing, if you KNOW the strings you want to eliminate, str_repl executes faster.

[edited by: CDNQuilter at 6:00 pm (utc) on May 28, 2007]

Roel




msg:3357633
 1:37 am on Jun 4, 2007 (gmt 0)

Hi CDNQuilter

Awesome! Works great!

Thank you so much!

I have one more question that you may be able to help me with:

My string may look like this:

word1, word2, word3, word4

I would like to make this:

word1, word2, word3 and word4

so the LAST comma in the string has to be swapped to " and "

I tried things like str_replace (', ', ' and ', $subject, -1) but this does not seem to work (maybe because I have PHP4)

Any ideas?

Thanks again for the preg help - now that I see it it makes more sense.

[edited by: Roel at 1:38 am (utc) on June 4, 2007]

Roel




msg:3362666
 5:36 am on Jun 9, 2007 (gmt 0)

Could anyone help me with this? Thanks!

Arno_Adams




msg:3362741
 9:14 am on Jun 9, 2007 (gmt 0)

Hi Roel,

To create a nice sentence, try this:


function makeLine($str) {
$a = explode(', ', $str);
for($i=0; $i<count($a)-1; $i++) {
$tmp.= $a[$i].', ';
}
$tmp = substr($tmp, 0, strlen($tmp)-2);
$tmp.= ' and '.$a[count($a)-1];
return $tmp;
}

HTH, AA

Roel




msg:3363974
 2:40 am on Jun 11, 2007 (gmt 0)

Hi AA

Thanks heaps to you as well!

So there was no easy solution like the "-1" :)

I used a modified version of your code in the header and footer of my pages.

Thanks again
Roel

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved