str_replace between two numerical strings
Posted
by user330840
on Stack Overflow
See other posts from Stack Overflow
or by user330840
Published on 2010-05-02T15:51:51Z
Indexed on
2010/05/02
15:57 UTC
Read the original article
Hit count: 152
str-replace
|php
Another problem with str_replace, I would like to change the following $title data into URL by taking the $string between number in the beginning and after dash (-)
- Chicago's Public Schools - $10.3M
- New Jersey - $3M
- Michigan: Public Health - $1M
The desire output is:
chicago-public-school
new-jersey
michigan-public-health
PHP code I am using
$title = ucwords(strtolower(strip_tags(str_replace("1: ","",$title))));
$x=1;
while($x <= 10) {
$title = ucwords(strtolower(strip_tags(str_replace("$x: ","",$title))));
$x++;
}
$link = preg_replace('/[<>()!#?:.$%\^&=+~`*é"\']/', '',$title);
$money = str_replace(" ","-",$link);
$link = explode(" - ",$link);
$link = preg_replace(" (\(.*?\))", "", $link[0]);
$amount = preg_replace(" (\(.*?\))", "", $link[1]);
$code_entities_match = array( ''s' ,'"' ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'<' ,'>' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'---' ,'--','--');
$code_entities_replace = array('' ,'-' ,'-' ,'' ,'' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'-' ,'-' ,'-','-');
$link = str_replace($code_entities_match, $code_entities_replace, $link);
$link = strtolower($link);
Unfortunately the result I got:
-chicagoamp9s-public-school
2-new-jersey
3-michigan-public-health
Anyone has a better solution for this? Thanks guys!
(the '
changed into amp9 - wonder why?)
© Stack Overflow or respective owner