str_replace 两个数字字符串之间的替换
str_replace
的另一个问题,我想通过获取开头和后面的数字之间的 $string
来将以下 $title
数据更改为 URL破折号 (-)
- 芝加哥
'
公立学校 - 1030 万美元 - 新泽西 - 300 万美元
- 密歇根:公共卫生< /strong> - $1M
期望的输出是:
芝加哥公立学校
新泽西州
michigan-public-health
我正在使用的 PHP 代码
$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);
不幸的是我得到的结果:
-chicagoamp9s-public-school
2-new-jersey
3-michigan-public-health
有人对此有更好的解决方案吗?谢谢大家!
('
变成了 amp9 - 想知道为什么吗?)
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', '"', '!', '@', '#', '
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?)
, '%', '^', '&', '*', '(', ')', '+', '{', '}', '|', ':', '"', '<', '>', '?', '[', ']', '', ';', "'", ',', '.', '_', '/', '*', '+', '~', '`', '=', ' ', '---', '--', '--');
$code_entities_replace = array('', '-', '-', '', '', '', '-', '-', '', '', '', '', '', '', '', '-', '', '', '', '', '', '', '', '', '', '-', '', '-', '-', '', '', '', '', '', '-', '-', '-', '-');
$link = str_replace($code_entities_match, $code_entities_replace, $link);
$link = strtolower($link);
Unfortunately the result I got:
Anyone has a better solution for this? Thanks guys!
(the '
changed into amp9 - wonder why?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我理解正确的话:
一件事:您的文本有“1.芝加哥...”而不是“1:...”,就像您的代码似乎建议的那样。是一个错误还是发生了其他事情?
If I understand you correctly:
One thing: your text has "1. Chicago's..." not "1: ..." like your code would seem to suggest. Is one an error or is there something else going on?
你可以这样做:
You can do:
和输出:
编辑开始:
输出:
希望它满足您的需求。
编辑结束。
and the output:
EDIT Start:
output:
Hope it meets your needs.
EDIT End.
假设您已经正确提取了“芝加哥公立学校”等标题,然后从中生成页面名称:
这会将
Chicago's“Public”:Scho-ols1+23
转换为
chicago-public-scho-ols1-23
。Assuming you already have extracted titles correctly like "Chicago's Public Schools", then to generate pagenames out of them:
Which will convert something like
Chicago's "Public": Scho-ols1+23
to
chicago-public-scho-ols1-23
.最后,我回顾了最初的代码并进行了一些修复:
我知道,这真是一团糟,但无论如何它都有效,谢谢大家帮助我,特别是 cletus,他发现了 1. 和 1 之间的错误:
Finally I looked back to initial code and have some fixes:
What a mess, I know, but it works anyway, thanks guys for helping me, especially cletus who found the mistake between 1. and 1: