PHP - Get Contents From Web By Preg_match
I want to parse some tiny lines from a webpage. I have few questions:
1. What is faster and better: cURL or file_get_contents? 2. Is it better to run preg_match commands form every line I want OR first get a shorter part (e.g. between <head> tag) then running preg_match for the lines? 3. Actually, I need to load the whole page before parsing. Is there a way to stop loading the whole page. For example, when I want something between <head> tag; stop loading <body>? Thank you in advance! Similar TutorialsHow to a check if a php variable value is one of the following characters: A to J, or ,k to t, (ie.: A B C D E F G H I J k l m n o p q r s t)
I tried:
if (preg_match[A-Jk-t], $checkChar) { // do this } in my site search some of the results come out still with html. i have used the strip tags function but how would i use the preg_match function to get rid of the "> still left over? Hi,
I have a "scraped" string and want to use the preg_match() to give me just a pice of it.
The string is from a webpage, but is dynamic....
here´s the typical string format:
<a class="stripped-link lightblue-link profile-page-link" href="/x/xxx+xxx+xxx/xxxx">HER IS THE WANTED TEXT</a>
The orange is static. The green (the xxxx is the dynamic part of the string)
Can I use the preg_match() for this?
thanks (and yes I´m a newbe.... )
Hi guys, I have been using this code: preg_match('/<h2>(.*?)<\/h2>/', $data, $matches); which i changed to: $tag1 = 'h2'; $tag2 = 'h2'; preg_match('/$tag1(.*?)$tag2/', $data, $matches); however i need it to work allowing variables (of any character or symbol) within $tag1. I tried $tag1 = 'h(.*?)2'; $tag2 = 'h2'; preg_match('/$tag1(.*?)$tag2/', $data, $matches); but that does not work .... essentiall the whole thing would work like this (example only): firstpartoftag1VARIABLE1lastpartoftag1VARIABLE2tag2 ps the preg_match should only capture the VARIABLE2 data not VARIABLE1 any ideas or help would be much appreciated cheers in advance Im trying to match this string, but i dont wat to match normal words like "hello world 646" Code: [Select] $string = "2ab055843241f20044af82ae0d5ace04"; //always over 15 chars - without spaces if ( preg_match('~^[0-9\s]+$~D', $string)) { echo 'yes'; } If i can do it with preg_match that would be great I have a DOMDocument which I inspect an HTML page for links (href) That part works however I am not sure how to use preg_match if I only want to display a link in this layout: <tr> <td headers="c1"><a title="Link to entity information." tabindex="1" href="CORPSEARCH.ENTITY_INFORMATION?p_nameid=3236937&p_corpid=3227476&p_entity_name=%41%72%77%65%6E%20%45%71%75%69%74%69%65%73&p_name_type=%41&p_search_type=%42%45%47%49%4E%53&p_srch_results_page=0">ABC LLC</a></td> </tr> I hope someone is really good at preg_match to help me. Thanks. Hi, I have the following code which FTPs to a site, gets the source code for a HTML file and then puts the data inbetween <TITLE> and </TITLE> into an array and this works fine: <?php $file = file_get_contents("ftp://user:pass@domain/public_html/index.html", "r"); if (!$file) { echo "<p>Unable to open remote file.\n"; exit; } preg_match_all ('/<TITLE>(.*)<\/TITLE>/msU', $file, $matches, PREG_SET_ORDER); print_r ($matches); ?> Rather than finding a match for data between <TITLE> and </TITLE> however I would like it to pull back what is inbetween the following: <!--START--> and <!--END--> I have tried the following but it doesn't put anything into the array: preg_match_all ('/<!--START-->(.*)<\/!--END-->/msU', $file, $matches, PREG_SET_ORDER); This is the content of the html file if this helps: Code: [Select] <TITLE>The Title</TITLE> <!--START--><p><strong>Some Text</strong></p> <p><strong><img src="images/IMAG0152_2.jpg" alt="" width="200" height="100" /></strong></p> <!--END--> i can get a match on stuff between TITLEs but not START and END.. I'm not sure how to change preg_match_all to work!! i've tried loads of combinations but can't get any to work, but i'm not really sure on modifiers and patterns or escaping etc, if anyone could help that would be great. Thanks in advance kev. i'm trying to grab the file name from mediafire to my database caption, I have rapidshare working fine. here's the code for rapidshare Code: [Select] if($type==1) { $words=$links[$j]; preg_match("/rapidshare\.com\/files\/\d+\/(.+)/",$words,$match); unset($words); $words=$match[1]; unset($match); $words=preg_split("/[_\.\-]/",$words); $lastword=array_pop($words); if($lastword=="html") array_pop($words); $words=implode(" ",$words); $words=preg_replace("/\s{2,}/"," ",$words); $caption=mysql_real_escape_string($words); unset($words); }How ever, i'm not sure which preg_match would be the correct one for mediafire.. appreciate any help. Hi im trying to do a registration form and i am new to using preg_match. I keep getting the error "( ! ) Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in C:\wamp\www\registration1.php on line 67" and for every other line I use preg_match. this is an example of how i have used it Code: [Select] if ( preg_match('^[[:alnum:]\.\'\-]{4,50}$', stripslashes(trim($_POST['UserName']))) ) { $UserName = mysql_real_escape_string($_POST['UserName']); $query = "SELECT UserName FROM Users WHERE UserName = '$UserName'"; $result = @mysql_query($query); $num = @mysql_num_rows($result); im such a noob at this can someone please help me I would also like to add that I have read the http://www.php.net/manual/en/function.preg-match.php page but i dont really understand it. Why dosent this work, im trying to match upper case, lower case and numbers only Code: [Select] $id = '3xDJ7@#'; if (!preg_match('/^[a-zA-Z0-9]/', $id)){ echo "ERROR!"; }else{ echo 'ok'; } This always echos "ok" Hey guys, In my contact form i have the action: preg_match but i've come accross a problem. Problem: Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in C:\Program Files (x86)\wamp\www\ts\contact.php on line 34 Code:$error_message = ""; $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; Thanks in advance I know $regex_pattern is wrong but I need a solution to find a content between <body> and </body>. Code: [Select] <?php $content = "<html><head><title>Your IP</title></head><body>Your IP Address: 63.1.142.154</body></html>"; $regex_pattern = "/<body>(.*)<\/body>/"; $preg_match($regex_pattern,htmlspecialchars($content),$matches); print_r( $matches ); ?> Hi,
I don't know much about php. Recently my host updated php version to 5.4 and i function eregi() is deprecated errors, so after some googling, replaced it with preg_match but i am stuck with errors
Code:
preg_match("^ and", "", $mid_str)
Error: Warning: preg_match(): No ending delimiter '^' found
Tried the following method but not working
preg_match("/^ and/", "", $mid_str)
Can you guys help me with correct code?
Thanks for looking.
I've tried alot of different ways, google, but just can't find the correct way to do this.. I'm trying to search for a word in an inputted string.. here's the code <html> <head><title>TESTING</title></head> <body> <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>"/> email: <input type=text name=email value="" /><br/> feedback: <input type=text name=ex value=""/><br/> <input type="submit" value="submit" /><br/> </body> </html> <?php $email = $_GET['email']; $ex = $_GET['ex']; $pattern = '/^[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z]+$/'; //$pattern1 = '/nprezident/prezident/'; if(!preg_match($pattern, $email)){ echo 'invalid email address<br/>'; } else { echo 'good to go<br/>'; } if(preg_match('/nprezident/,/prezident/', $ex)){ echo 'just something about that sentence is nice'; } else { echo 'no good'; } ?> Ok the one I'm having a problem with is the second preg_match when trying to search for two words i can get one word to print but when i use add a second word it gives an error. How do i add a second word? I need to select an array item based on my preg result. for example: Code: [Select] $content = 'test-1'; $match = preg_replace('|test-([0-9])|', $array['\\1'], $content); is this possible? I'm pretty new to this, How exactly could i get the text between the www. and .com? so if i have a url: http://www.facebook.com I want to just return "facebook" information.txt <li><img src="http://www.lol.com" alt="0" /></li> <li><img src="http://www.lol.com" alt="1" /></li> <li><img src="http://www.lol.com" alt="4" /></li> <li><img src="http://www.lol.com" alt="9" /></li> <li><img src="http://www.lol.com" alt="1" /></li> <li><img src="http://www.lol.com" alt="7" /></li> <li><img src="http://www.lol.com" alt="2" /></li> How can I get the alt="$ValueIwant" for each with a preg_match im trying to make a script so i can get users details like longitude, latitude etc
but im having trouble matching the html by regular expression
here is the html below that im trying to extract the data from:
any help or possible any alternative, suggested ways would be greatful thank you
<td>Country:</td> <td><img src="/images/dot.gif" class="flag-16 gb" align="absmiddle" width="16" height="16" title="United Kingdom"> United Kingdom (GB)</td> </tr> <tr> <td>City:</td> <td>Newport</td> </tr> <tr> <td>Region:</td> <td>Newport</td> </tr> <tr> <td>Latitude:</td> <td>51.5833</td> </tr> <tr> <td>Longitude:</td> <td>-2.9833</td> </tr> <tr> <td>Timezone:</td> <td>Europe/London</td> <?php $url = "http://smart-ip.net/geoip/2.101.108.124/"; $ch = curl_init(); $timeout = 0; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $rawdata = curl_exec($ch); curl_close($ch); if (preg_match('/<td>Country:</td><td>[img](?P<country>\w+)</td>/$', $rawdata, $match)) { $country = $match['country']; } if (preg_match('/<td>City:</td><td>(?P<city>\w+)</td>/$', $rawdata, $match)) { $city = $match['city']; } if (preg_match('/<td>Region:</td><td>(?P<region>\w+)</td>/$', $rawdata, $match)) { $region = $match['region']; } if (preg_match('/<td>Latitude:</td><td>(?P<latitude>\d+)</td>/$', $rawdata, $match)) { $latitude = $match['latitude']; } if (preg_match('/<td>Longitude:</td><td>(?P<longitude>\d+)</td>/$', $rawdata, $match)) { $longitude = $match['longitude']; } if (preg_match('/<td>Timezone:</td><td>(?P<timezone>\w+)</td>/$', $rawdata, $match)) { $timezone = $match['timezone']; } ?> Hello, I am a bit confused on the preg_match subject. here is a snippet where I use the preg_match func: Code: [Select] <?php foreach ($bans as $ban){ $curbans = $ban["user_steamid"]; if(isset($_POST['submit2'])) { $_SESSION['isbanned'] = preg_match('/'.$_SESSION['inputid'].'/i', $curbans) } }; ?> The array, $curbans, looks like this Code: [Select] STEAM_0:1:25941882STEAM_0:1:37863673STEAM_0:0:49096012STEAM_0:0:31113930 How would I get it to match something like "STEAM_0:0:49096012" with a value in the array? Hi, I need a preg_match for the following criteria: - no leading or trailing white space allowed - only alphanumeric characters and spaces are allowed (no full stops, commas apostrophe's, explanation marks, quotes, etc) - only one space allowed between each word (if there is more than one word) Thanks. |