0

のもう 1 つの問題として、次のデータを先頭とダッシュ (-) の後の数字の間をとって URLstr_replaceに変更したいと思います。$title$string

  1. シカゴ'の公立学校- $10.3M
  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

誰にもこれに対するより良い解決策がありますか? みんなありがとう!
(が amp9&#39;変更されました - なぜだろうか? )

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 つのこと: あなたのテキストには、コードが示唆するように「1: ...」ではなく「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 に答える