0

另一个问题str_replace,我想将以下$title数据更改为 URL,方法是$string在开头和破折号 (-) 之后取中间数字

  1. 芝加哥'公立学校- 1030 万美元
  2. 新泽西州- 300 万美元
  3. 密歇根州:公共卫生- 100 万美元

愿望输出是:
芝加哥-公立-学校
-新泽西
-密歇根-公共卫生

我正在使用的 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('/[<>()!#?:.$%\^&=+~`*&#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);

不幸的是我得到的结果:

-chicagoamp9s-public-school
2-new-jersey
3-michigan-public-health

有人对此有更好的解决方案吗?多谢你们!
(更改&#39;amp9 - 想知道为什么?

4

5 回答 5

0

最后,我回顾了初始代码并进行了一些修复:

$title = ucwords(strtolower(strip_tags(str_replace("1. ","",$title))));
$x=1;
while($x <= 10) {
$title = ucwords(strtolower(strip_tags(str_replace("$x. ","",$title))));
$x++;
}
$data = preg_replace('/[<>()!#?:.$%\^&=+~`*&#;"\']/', '',$title);
$urldata = str_replace(" ","-",$data);
$data = explode(" - ",$data);
$link = preg_replace(" (\(.*?\))", "", $data[0]);
$budget = preg_replace(" (\(.*?\))", "", $data[1]);
$code_entities_match = array( '&quot;' ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'<' ,'>' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'---' ,'--','--');
$code_entities_replace = array('' ,'-' ,'-' ,'' ,'' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'-' ,'-' ,'-','-');
$link = str_replace($code_entities_match, $code_entities_replace, $link);
$link = strip_tags(str_replace("amp39s","",$link));
$link = strtolower($link);

真是一团糟,我知道,但它仍然有效,感谢帮助我的人,特别是发现 1. 和 1 之间错误的 cletus:

于 2010-05-02T17:41:08.100 回答
0

如果我理解正确:

if (preg_match('!\d+:\s+(.*)\s+-\s+\$\d+(?:\.\d+)?!', $title, $groups)) {
  $words = strip_tags(strtolower($groups[1]));
  $words = preg_replace('\[^\s\w]!', '', $words);
  $words = preg_replace('!\s+!', '-', $words);
  $words = preg_replace('!-+!', '-', $words);
  echo $words;
}

一件事:您的文本有“1. Chicago's ...”而不是“1:...”,就像您的代码似乎暗示的那样。是一个错误还是发生了其他事情?

于 2010-05-02T16:03:55.250 回答
0

你可以做:

$str = "1. Chicago's Public Schools - $10.3M";
$from = array('/^\d+\.\s+([^-]*) -.*$/','/[^A-Z ]/i','/\s+/');
$to = array("$1",'','-');
$str = strtolower(preg_replace($from,$to,$str));
echo $str; // prints chicagos-public-schools
于 2010-05-02T16:05:18.763 回答
0

假设您已经正确提取了“芝加哥公立学校”等标题,然后从中生成页面名称:

function generatePagename($s) {
    //to lower
    $pagename = trim(html_entity_decode(strtolower($s), ENT_QUOTES));

    //remove 's
    $pagename = trim(preg_replace("/(\'s)/", "", $pagename));

    //replace special chars with spaces
    $pagename = trim(preg_replace("/[^a-z0-9\s]/", " ", $pagename));

    //replace spaces with dashes
    $pagename = trim(preg_replace("/\s+/", "-", $pagename));

    return $pagename;
}

这将转换为

Chicago&#39;s "Public": Scho-ols1+23

chicago-public-scho-ols1-23.

于 2010-05-02T16:17:06.680 回答
0
<?php

$lines = array("1. Chicago's Public Schools - $10.3M",
                "2. New Jersey - $3M",
                "3. Michigan: Public Health - $1M"
            );

// remove the number bullets
$lines = preg_replace('/\ - \$\d*\.?\d*M$/', '', $lines);

// remove the trailing dollar amount
$lines = preg_replace('/^\d+\.\ /', '', $lines);

// remove ignore chars 
$ignore_pattern = "/['s|:]/";
$lines = preg_replace($ignore_pattern, '', $lines);

for ($i=0; $i<count($lines); $i++) {
    $lines[$i] = implode('-',explode(' ', strtolower(trim($lines[$i]))));
}

print_r($lines);

和输出:

Array
(
    [0] => chicago-public-school
    [1] => new-jerey
    [2] => michigan-public-health
)

编辑开始:

<?php

$lines = array("1. Chicago's Public Schools - $10.3M",
                "2. New Jersey - $3M",
                "3. Michigan: Public Health - $1M",
                "4. New York's Starbucks - $2M",
            );

$lines = preg_replace('/\ - \$\d*\.?\d*M$/', '', $lines);

$lines = preg_replace('/^\d+\.\ /', '', $lines);

$ignore_strings = array("'s", ':');
for ($i=0; $i<count($lines); $i++) {
    foreach ($ignore_strings as $s) {
        $lines[$i] = str_replace($ignore_strings, '', $lines[$i]);
    }
}

for ($i=0; $i<count($lines); $i++) {
    $lines[$i] = implode('-',explode(' ', strtolower(trim($lines[$i]))));
}

print_r($lines);

输出:

Array
(
    [0] => chicago-public-schools
    [1] => new-jersey
    [2] => michigan-public-health
    [3] => new-york-starbucks
)

希望它能满足你的需求。编辑结束。

于 2010-05-02T16:22:42.253 回答