比较字符串并删除部分字符串

I have strings like this

$title = "1 Gal. Black PE Grow Bag (100 per pack)";

$quantity = "10";

I want to check is string title contains string quantity. If dont then delete text in brackets, if yes title will be the same. I did something like this, but dont work good for me:

  if (strpos($title, quantity) !== false) {

    }
else{
   $title = preg_replace('/\([\s\S]+?\)/', '', $title);
}

In this example quantity is not contained in the string title. There is number 100 in the string title, but that is not the same, but i should add condition for that also??

You are actually very close. All you need is a space in your code and it will work.
Notice the space in the strpos.
This will make sure it's 10 and not 10 in 100 since we have a space after the 10.

$title = "1 Gal. Black PE Grow Bag (100 per pack)";

$quantity= "10";
if (strpos($title, $quantity . " ") !== false) {

}else{
    $title = preg_replace('/\([\s\S]+?\)/', '', $title);
}
echo $title;

I created a demo of how it handles 10/100.

https://3v4l.org/ZdYFk

You can use this regex since its a small simple string..

\(.*\)

Also, I like to use online regex editors such as https://www.phpliveregex.com/

You can see it in action on your code here https://www.phpliveregex.com/p/pSx

So the whole code can look like this

if ($quantity === 0){
$title = preg_replace("/\(.*\)/","", $title);
}

this is assuming you use 0 when the item is out of stock.

I made this into a working function:

<?php
    function checkQty($title, $qty)
    {
        $words = explode(' ', $title);
        $endKey = end(array_keys($words));

        foreach ($words as $key => $word)
        {
            if (strpos($word, '(') !== false) {
                $word = str_replace('(', '', $word);
            }

            if (is_numeric($word)) {
                $word = (int) $word;
            }

            if ((int) $qty === $word) {
                $toDelete = range($key, $endKey);
            }
        }

        if (!empty($toDelete)) {
            foreach ($toDelete as $key => $del)
            {
                unset($words[$del]);
            }
        }

        return trim(implode(' ', $words));
    }

    $title = "1 Gal. Black PE Grow Bag (100 per pack)";
    $quantity = "100";

    echo '<pre>'. print_r(checkQty($title, $quantity), 1) .'</pre>';

    $quantity = '10';
    echo '<pre>'. print_r(checkQty($title, $quantity), 1) .'</pre>';

this goes through each word in the title, if the string is a numeric value, convert to int and compare to the quantity. If quantity matches the value, create a range of the key to the end key of the exploded array and delete if $toDelete contains values.

You should turn $quantity into a regular expression, then you can use the word boundary escape sequences to make it match a whole word, not part of a word.

$quantity = '/\b10\b/';
if (!preg_match($quantity, $title)) {
    $title = preg_replace('/\(.+?\)/s', '', $title);
}

And instead of using [\s\S] in the regexp, you can use . along with the s modifier; that makes . match anything, including newlines.