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);
With my code I take this: in ('463','267_462'), I try with preg_replace remove if exists all character before _ ($ids = preg_replace('/^[^]*\s*/', '', $ids);)
I need this return: in ('267','463','462')
I can't get what you need to return. If you need an array, you can try this:
$result = array_map(function (string $item) {
return preg_replace('.+_', '', $item);
}, explode(',', $categoriesInArticle));
also, you can implode(',', $result)
to get string w/o _
sign.
Is some thing like this you want ? 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)."'";
}