php - Use preg_replace() to Isolate Number in Source Code -
i trying pick out single value (in instance, value 544007664) source code of page pulling of youtube. right now, script have loading source code youtube page , removing "<" , ">" symbols when echo source code, displays text , doesn't display page itself. 2 preg_replace() functions (here on lines 4 , 5) pull out before , after desired value not doing expect them do.
the thing can think of preg_replace() refusing read single string text in reality several dozens of individual lines.
<?php $str = file_get_contents('https://www.youtube.com/watch?v=5xr7naz_zza'); $str = $str; $str = preg_replace('~^(.)+(meta name="twitter:app:id:ipad" content=")~', '', $str); $str = preg_replace('~(" meta name="twitter:app:url:iphone")+(.)$~', '', $str); $str = preg_replace('~<~', '', $str); $str = preg_replace('~>~', '', $str); echo $str; ?>
any appreciated.
i think you're on right track one, overthinking bit.
here's simple snippet can use numbers:
$str = file_get_contents('https://www.youtube.com/watch?v=5xr7naz_zza'); $app_store_id = preg_replace('~.*?<meta property="al:ios:app_store_id" content="(\d+)">.*~si', '$1', $str); print $app_store_id;
this outputs:
544007664
here explanation of regex:
.*?
- match character.
, number of times*
, until hits next portion of regex string?
. next part specific<meta ...
tag looking for.<meta property="al:ios:app_store_id" content="
- locates meta tag "app_store_id" in it.(\d+)
- grabbing @ least 1+
digit\d
. put in parenthesis because assigned$1
. use value of$1
replace contents of string. (so replacing entire web page number found.)">
- finishing off meta tag..*
- match character.
, number of times*
after meta tag.
note using s
flag after expression make newlines count character. let search across multiple lines. often, s
flag paired m
flag, although in case wasn't necessary.
here link page listing different flags can use in php.
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
Comments
Post a Comment