function categoriesIdsFunction($ids){
//I try with preg_replace remove if exists all character before _
$ids = preg_replace('/^[^_]*_\s*/', '', $ids);
$ids = preg_replace('/\s+/', '', $ids);
return "in ('" . str_replace(",","','",$ids) . "') ";
}
$categories_in_article = "267,267_463,267_462";
$categories_in_article_return = categoriesIdsFunction($categories_in_article);
通过我的代码,我接受了这一点:(' 463',' 267_462'), 我尝试使用preg_replace remove如果在_之前存在所有字符($ ids = preg_replace(' / ^ [^ ] * \ s * /','&#39 ;,$ ids);)
我需要这个回报:在(' 267',' 463',' 462')
答案 0 :(得分:1)
我无法得到你需要返回的东西。如果你需要一个数组,你可以试试这个:
$result = array_map(function (string $item) {
return preg_replace('.+_', '', $item);
}, explode(',', $categoriesInArticle));
另外,您可以implode(',', $result)
获取不带_
符号的字符串。
答案 1 :(得分:0)
你想要的是这样吗?
out put:in ('267','463','462')
function test2()
{
$categories_in_article = "267,267_463,267_462";
$stringArray = explode ( "," , $categories_in_article);
$categories_in_article_return = "in (";
foreach($stringArray as $string){
$categories_in_article_return .=
$this->categoriesIdsFunction($string).",";
}
$categories_in_article_return = rtrim($categories_in_article_return,",");
$categories_in_article_return .= ") ";
echo $categories_in_article_return;
}
function categoriesIdsFunction($ids){
//I try with preg_replace remove if exists all character before _
$ids = preg_replace('/^[^_]*_\s*/', '', $ids);
$ids = preg_replace('/\s+/', '', $ids);
return "'".str_replace(",","','",$ids)."'";
}