0

[caption id="attachment_1342" align="alignleft" width="300" caption="Cheers... "Forward" diversifying innovation to secure first place. "][/caption] A group of 35 students from...

I'm reading this data from api. I want the text just start with A group of 35 students from.... Help me to replace the caption tag with null. This is what I tried:

echo "<table>";
echo "<td>".$obj[0]['title']."</td>";
echo "<td>".$obj[0]['content']."</td>";
echo "</table>";
$html = $obj[0]['content'];
preg_match_all('/<caption>(.*?)<\/caption>/s', $html, $matches);
preg_replace('',$matches, $obj[0]['content']);

Any help.

4 Answers 4

1
$pattern = "/\[caption (.*?)\](.*?)\[\/caption\]/i";
$removed = preg_replace($pattern, "", $html);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I use explode function to do this. $html = $obj[0]['content']; $code = (explode("[/caption]", $html)); if($code[1]==''){ echo $code[1]; }
1
 echo preg_replace("#\[caption.*\[/caption\]#u", "", $str);

1 Comment

Thank you. I use explode function to do this. $html = $obj[0]['content']; $code = (explode("[/caption]", $html)); if($code[1]==''){ echo $code[1]; }
0

In the snippet mentioned in the question, regex search pattern is incorrect. there is no <caption> in the input. its <caption id....

Second using preg_replace doesn't serve any purpose here. preg_replace expects three arguments. first should be a regex pattern for search. second the string to replace with. and third is input string.

Following snippet using preg_match will work.

<?php

//The input string from API
$inputString = '<caption id="attachment_1342" align="alignleft" width="300" caption="Cheers... "Forward" diversifying innovation to secure first place. "></caption> A group of 35 students from';

//Search Regex
$pattern = '/<caption(.*?)<\/caption>(.*?)$/';

//preg_match searches inputString for a match to the regular expression given in pattern
//The matches are placed in the third argument.
preg_match($pattern, $inputString, $matches);

//First match is the whole string. second if the part before caption. third is part after caption.
echo $matches[2];
// var_dump($matches);

?>

if you still want to use preg_match_all for some reason. following snippet is modification of the one mentioned in question -

<?php

//Sample Object for test
$obj = array(
    array(
        'title' => 'test',
        'content' => '<caption id="attachment_1342" align="alignleft" width="300" caption="Cheers... "Forward" diversifying innovation to secure first place. "></caption> A group of 35 students from'
    )
);

echo "<table border='1'>";
echo "<td>".$obj[0]['title']."</td>";
echo "<td>".$obj[0]['content']."</td>";
echo "</table>";

$html = $obj[0]['content'];

//preg_match_all will put the caption tag in first match
preg_match_all('/<caption(.*?)<\/caption>/s', $html, $matches2);

//var_dump($matches2);

//use replace to remove the chunk from content
$obj[0]['content'] = str_replace($matches2[0], '', $obj[0]['content']);

//var_dump($obj);

?>

1 Comment

Thank you. I use explode function to do this. $html = $obj[0]['content']; $code = (explode("[/caption]", $html)); if($code[1]==''){ echo $code[1]; }
0

Thank you guys. I use explode function to do this.

$html = $obj[0]['content'];
    $code = (explode("[/caption]", $html));
    if($code[1]==''){
    echo $code[1];  
    }   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.