内容与文本字符串隔离

时间:2011-04-22 09:49:55

标签: php string text-processing

我将"&params=&offer=art-by-jeremy-johnson"存储在我的数据库中。

是否有任何函数/方法可以使用上面的输入值将输出作为"Art by Jeremy Johnson"。这应仅在运行时更改为输出"Art by Jeremy Johnson"

可以用PHP完成。

请帮忙。

4 个答案:

答案 0 :(得分:1)

$orig = '&params=&offer=art-by-jeremy-johnson';
$parts = explode('=', $orig);
$output = explode('-', end($parts));
echo ucwords(implode(' ', $output));

答案 1 :(得分:1)

在Java中,我想您可以使用lastIndexOf来获取等号的最后一个索引,并获取字符串的其余部分(使用substring)。

if (myString.lastIndexOf("=") != -1) {
   String words = myString.substring(myString.lastIndexOf("=")+1);
   words.replaceAll("-", " ");
   return words;
}

答案 2 :(得分:1)

$string="&params=&offer=art-by-jeremy-johnson";

parse_str($string,$output);
//print_r($output);
$str=ucwords(str_replace("-"," ",$output['offer']));

答案 3 :(得分:0)

如果我理解得很好,你就不想把一些词语大写。

这是一种方法:

$str = "&params=&offer=art-by-jeremy-johnson";

// List of words to NOT capitalized
$keep_lower = array('by');

parse_str($str, $p);
$o = explode('-', $p['offer']);
$r = array();
foreach ($o as $w) {
    if (!in_array($w, $keep_lower))
        $w = ucfirst($w);
    $r[] = $w;
}
$offer = implode(' ', $r);
echo $offer,"\n";

<强>输出:

Art by Jeremy Johnson