开发者

PHP - MySQLi Replace with Regex / Regexp / Regular Expression

Sorry for that "Let me google that for you"-Question, but I can't find an answer.

I want to delete specfic elements from columns in my database. So I search with the following code for them: (it works fine)

    $sql = "SELECT list_id, list_domains
            FROM list
            WHERE list_domains REGEXP '[\s]*[a-z-]*\.[a-z/\.\?=]*.#".$number."#'";
  开发者_开发技巧      $result = $db->prepare( $sql );
        $result->execute();
        $result->bind_result( $list_id, $list_domains );
        $result->store_result();

Now I want to delete / replace the found elements inside these columns. So I use the following code:

 $sql = "UPDATE list
        SET list_domains = REPLACE(list_domains, '[\s]*[a-z-]*\.[a-z/\.\?=]*.#".$number."#', '')
        WHERE list_domains REGEXP '[\s]*[a-z-]*\.[a-z/\.\?=]*.#".$number."#'";
        $result = $db->prepare( $sql );
        $result->execute();
        $result->store_result();

Does not work. I also tried

REPLACE(list_domains, REGEXP '[\s]*[a-z-]*\.[a-z/\.\?=]*.#".$number."#', '') 

or

REGEXP_REPLACE(list_domains, '[\s]*[a-z-]*\.[a-z/\.\?=]*.#".$number."#', '') 

but these lines only produce errors.

How does it work to delete / replace specfic regular expressions with MySQLi?

Thanks for every suggestion!


REPLACE() doesn't allow regular expression searching.

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

If you want to manipulate strings with regex, SELECT the rows, do the manipulation in your own code, then issue an UPDATE statement to update the value, specifying the final value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜