Generating backslash in a string in PHP

Viewed 49

I have the following string that the ultimate goal is to use it in like sql. The output should be '%"cardSelected":"120-B-110-C"%' .

But the output of this string is generating with backslashes like this below '%\"cardSelected\":\"120-B-110-C\"%'

$search_term = '"cardSelected":"120-B-110-C"';
$like =  '%'. $search_term .'%';
        
$sql = $wpdb->prepare(
       "SELECT * FROM $wpdb->posts WHERE post_status = %s AND post_content LIKE %s;",
       'publish',
       $like,
);

I tried using str_replace as below, but the slashes were not removed either

$search_term = '"cardSelected":"120-B-110-C"';
$like =  str_replace('\\','','%'. $search_term .'%');
0 Answers
Related