Skip to content
Advertisement

Laravel find and replace content on the basis of a expression in content coming from Db

I have a blog content in which admin can add pattern like this => %make123%. when that content is to be displayed i have to replace that %make123% with the vehicle id of 123 but now how to get that 123 from my content. and then make another query to search the vehicle with that id.

this is my controller function for blog detail.

function blogDetail($blog_id, $title_link){

$dataSetArray = array();
$dataSetArray['route'] = '/blog/:blog_title';

$rs = "SELECT id, post_title, post_meta_title, post_author, post_meta_description, 
post_meta_keywords, 
post_content, post_image, DATE_FORMAT(created_at, '%d-%b-%Y') as created_at FROM blog_post
where id = '$blog_id' AND CONCAT( CONCAT( CONCAT( SUBSTRING(MY_TITLE(post_title), 1, 50),'-'), 
id),'.html') = '$title_link' ";
$results = DB::select($rs);





$results = json_decode(json_encode($results), true);
foreach ($results as $key => $value){
  $content =  $value['post_content'];
}

$pattern = '/%make(.+?)%/';

preg_match_all($pattern,$content,
$out, PREG_PATTERN_ORDER);


}

Backend is in laravel and frontend is in react js.

Advertisement

Answer

Use regex to get your content id from the given string.

$regex = '/(%)([a-zA-Z]*)([0-9]*)(%)/m';
    
preg_match_all($regex, $content, $matches, PREG_SET_ORDER, 0);
    
// Print the entire match result
echo "<pre>";
print_r($matches[0][3]);
echo "</pre>";
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement