str_replace 两个数字字符串之间的替换

发布于 2024-08-30 21:55:37 字数 1620 浏览 5 评论 0原文

str_replace 的另一个问题,我想通过获取开头和后面的数字之间的 $string 来将以下 $title 数据更改为 URL破折号 (-)

  1. 芝加哥'公立学校 - 1030 万美元
  2. 新泽西 - 300 万美元
  3. 密歇根:公共卫生< /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('/[<>()!#?:.$%\^&=+~`*&#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 - 想知道为什么吗?

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('/[<>()!#?:.$%\^&=+~`*é"\']/', '', $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半枫 2024-09-06 21:55:37

如果我理解正确的话:

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.芝加哥...”而不是“1:...”,就像您的代码似乎建议的那样。是一个错误还是发生了其他事情?

If I understand you correctly:

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;
}

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?

放血 2024-09-06 21:55:37

你可以这样做:

$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

You can do:

$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
╰◇生如夏花灿烂 2024-09-06 21:55:37
<?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
)

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

<?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);

and the output:

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

EDIT Start:

<?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);

output:

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

Hope it meets your needs.
EDIT End.

意中人 2024-09-06 21:55:37

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

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

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;
}

Which will convert something like

Chicago's "Public": Scho-ols1+23

to

chicago-public-scho-ols1-23.

酒几许 2024-09-06 21:55:37

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

$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( '"' ,'!' ,'@' ,'#' ,'

我知道,这真是一团糟,但无论如何它都有效,谢谢大家帮助我,特别是 cletus,他发现了 1. 和 1 之间的错误:

,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'<' ,'>' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'---' ,'--','--'); $code_entities_replace = array('' ,'-' ,'-' ,'' ,'' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'-' ,'-' ,'-','-'); $link = str_replace($code_entities_match, $code_entities_replace, $link); $link = strip_tags(str_replace("amp39s","",$link)); $link = strtolower($link);

我知道,这真是一团糟,但无论如何它都有效,谢谢大家帮助我,特别是 cletus,他发现了 1. 和 1 之间的错误:

Finally I looked back to initial code and have some fixes:

$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( '"' ,'!' ,'@' ,'#' ,'

What a mess, I know, but it works anyway, thanks guys for helping me, especially cletus who found the mistake between 1. and 1:

,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'<' ,'>' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'---' ,'--','--'); $code_entities_replace = array('' ,'-' ,'-' ,'' ,'' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'-' ,'-' ,'-','-'); $link = str_replace($code_entities_match, $code_entities_replace, $link); $link = strip_tags(str_replace("amp39s","",$link)); $link = strtolower($link);

What a mess, I know, but it works anyway, thanks guys for helping me, especially cletus who found the mistake between 1. and 1:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文