0

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);
?>

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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 )

4 Comments

Your first code sample you pushed, had a blank pattern and did not work. Your code snippet now, doesn't show why his code isn't working. Code blocks without explanations do little to help future readers understand why the code wasn't working correctly.
I accepted without explanations part but even $pattern = '' still work because I write directly inside preg_match.
The code was broken at that point (I didn't recall exactly why, because the history of this post isn't showing edits because you deleted/undeleted the answer), but when I copied/pasted it into a php fiddle, it didn't work.
I still keep the first version because I write in phptestter.net and not close yet and I sure that works fine. Next time I hope your hands do not faster than your brain.

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.