获取字符串的前导部分并转换为短横线大小写
我正在尝试将标题数据更改为 URL。我需要从 $title 获得的以下动态数据中删除从“截止日期”开始的所有文本:
示例:
纽约截止日期 2010 年 5 月 14 日(紧急)
新罕布什尔截止日期2010 年 5 月 19 日
新泽西截止日期
我预计结果应该是这样的:
new-york
new-hampshire
new-jersey
这是我尝试过的代码
$newurl = strip_tags(str_replace("deadline","","$title"));
$code_entities_match = array( '"' ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'<' ,'>' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'---' ,'--','--');
$code_entities_replace = array('' ,'-' ,'-' ,'' ,'' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'-' ,'-' ,'-','-');
$newtitle = str_replace($code_entities_match, $code_entities_replace, $newurl);
$urltitle = strtolower($newtitle);
不幸的是,结果是:
new-york-deadline-may-14th-2010-urgent
new -hampshire-deadline-may-19th-2010
新泽西州-
I am trying to change title data into URL. I need to get rid off all text starting from "deadline" from the following dynamic data which I get from $title:
Examples:
New York deadline May 14th, 2010 (urgent)
New Hampshire deadline May 19th, 2010
New Jersey deadline
I expect the results should be like this:
new-york
new-hampshire
new-jersey
Here is the code I have tried
$newurl = strip_tags(str_replace("deadline","","$title"));
$code_entities_match = array( '"' ,'!' ,'@' ,'#' ,'
Unfortunately, the results are:
new-york-deadline-may-14th-2010-urgent
new-hampshire-deadline-may-19th-2010
new-jersey-
,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'<' ,'>' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'---' ,'--','--');
$code_entities_replace = array('' ,'-' ,'-' ,'' ,'' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'-' ,'-' ,'-','-');
$newtitle = str_replace($code_entities_match, $code_entities_replace, $newurl);
$urltitle = strtolower($newtitle);
Unfortunately, the results are:
new-york-deadline-may-14th-2010-urgent
new-hampshire-deadline-may-19th-2010
new-jersey-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将 preg_replace 用作:
You can use preg_replace as:
这是一种非正则表达式的方式
Here's a non-regex way
要从美国州引导 inout 字符串中生成所需的 kebab-case 字符串:
代码:(Demo)
如果您的输入可能包含连续的空格、连字符或其他符号,则实现将需要更复杂的处理
To produce the desired kebab-case string from your US state lead inout strings:
Code: (Demo)
If your input might contain consecutive spaces, hyphens, or other symbols, the implementation would need more intricate handling