PHP接受任何号码

geo*_*ous 0 php wordpress

我在帖子中有这个代码:

[quick_view product_id="10289" type="button" label="Quick View"]
Run Code Online (Sandbox Code Playgroud)

我希望函数内部的数字"10289"匹配任何数字:

if (stripos($post-> post_content, '[quick_view product_id="XXXX" type="button" label="Quick View"]') !== false) 
Run Code Online (Sandbox Code Playgroud)

如何更换"XXXX"接受所有号码?

完整片段:

function conditionally_add_scripts_and_styles($posts){
if (empty($posts)) return $posts;
$shortcode_found = false; // use this flag to see if styles and scripts   need to be enqueued
foreach ($posts as $post) {
if (stripos($post-> post_content, '[quick_view product_id="XXXX" type="button" label="Quick View"]') !== false) {
$shortcode_found = true; // bingo!
break;
}
}
if ($shortcode_found) {
// enqueue here
wp_enqueue_style('my-style', '/woocommerce.css');
wp_enqueue_script('my-script', '/script.js');
}
return $posts;
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Sah*_*ati 9

试试这个,希望它能正常工作.

/product_id\s*\=\s*\"\d+\"/正则表达式:此正则表达式将查找product_id="< - > 之间的数字"

例子:

product_id="xxAbcxx" 将被拒绝.

product_id="1212121" 将被接受.

if(preg_match("/product_id\s*\=\s*\"\d+\"/", $post->post_content))
{
    echo "Accepted";   
}
Run Code Online (Sandbox Code Playgroud)

您的完整代码将如下所示.

function conditionally_add_scripts_and_styles($posts)
{
    if (empty($posts))
        return $posts;
    $shortcode_found = false; // use this flag to see if styles and scripts   need to be enqueued
    foreach ($posts as $post)
    {
        if(preg_match("/product_id\s*\=\s*\"\d+\"/", $post->post_content))
        {
            $shortcode_found = true;   
            break;
        }
    }
    if ($shortcode_found)
    {
// enqueue here
        wp_enqueue_style('my-style', '/woocommerce.css');
        wp_enqueue_script('my-script', '/script.js');
    }
    return $posts;
}
Run Code Online (Sandbox Code Playgroud)