Q: preg_match to retrieve string I need to retrieve the data-videoid ( h7rhqXbdIts )
<?php
$pattern = '\'/data-videoid="([^"]+)/\'';
$subject = '<amp-youtube width="640" height="360" data-videoid="h7rhqXbdIts" layout="responsive"></amp-youtube>';
$result = preg_match( $pattern, $subject , $matches );
echo $result;
print_r($matches);
?>


A: You added some ' single quotes at the start/end of your pattern, which is attempting to do a literal match for the quotes (Which don't exist). Simply removing them should solve your issue:
<?php
$pattern = '/data-videoid="([^"]+)/';
$subject = '<amp-youtube width="640" height="360" data-videoid="h7rhqXbdIts" layout="responsive"></amp-youtube>';
$result = preg_match( $pattern, $subject , $matches );
echo $result;
print_r($matches);
?>

See this output where you can see that $matches[1] contains the videoid you're looking for (h7rhqXbdIts).

A: This code should work
$subject = '<amp-youtube width="640" height="360" data-videoid="h7rhqXbdIts" layout="responsive"></amp-youtube>';
$result = preg_match( '@data-videoid="(.*?)"@s', $subject , $matches );
echo $result;
print_r($matches); //Array ( [0] => data-videoid="h7rhqXbdIts" [1] => h7rhqXbdIts )

