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: 159

Filed under:
|

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 (-)

  1. Chicago's Public Schools - $10.3M
  2. New Jersey - $3M
  3. 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('/[<>()!#?:.$%\^&=+~`*&#233;"\']/', '',$title);
$money = str_replace(" ","-",$link);
$link = explode(" - ",$link);
$link = preg_replace(" (\(.*?\))", "", $link[0]);
$amount = preg_replace(" (\(.*?\))", "", $link[1]);
$code_entities_match = array( '&#39;s' ,'&quot;' ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'<' ,'>' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'---' ,'--','--');
$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 &#39; changed into amp9 - wonder why?)

© Stack Overflow or respective owner

Related posts about str-replace

Related posts about php