This question already has answers here:
Using an array as needles in strpos
(16 answers)
Closed 9 years ago.
Im running strpos on a <a> tag to see if it contains either one of two urls.
At the moment im using this bellow - how would i set it to check if - tumblr.com OR google.com were present ?
function find_excluded_url ($url) {
$find = "tumblr.com"; // OR GOOGLE.COM ....
$pos = strpos($url, $find);
if ($pos === false) {
return false;
} else {
return true;
}
}
// SET URL
$url = "<a href='http://tumblr.com/my_post' rel='nofollow'>This site</a>";
// CALL FUNC
$run_url = find_excluded_url($url);
if ($run_url == true) {
echo "URL - " . $url . "<br>";
}
You can't use two needles in strpos. But what you can do, is use it twice, with an or:
function find_excluded_url ($url) {
return (strpos($url, "tumblr.com")!==false) || (strpos($url, "google.com")!==false);
}
For more complicated searches, you might want to look into Regular Expressions. This would work:
$subject = 'blablabgoogle
balblabtumblrasd
blaasdgoogleadsad';
$pattern = '#(?:google\.com|tumblr\.com)#i';
$result = preg_match($pattern, $subject, $subpattern, PREG_OFFSET_CAPTURE);
if($result) echo 'Position: ' . $subpattern[0][1];
The performance of this (if performance is an issue for you) depends on how many search queries you have and how big your haystack is. Regular expressions come with a relatively big overhead, however, they only have to run over the text once. If you use strpos twice, this gets expensive with long strings. If performance is really an issue, you could also write your own strpos that goes character per character. I doubt, however, that this is necessary.
function find_excluded_url ($url, $searchURL) {
$pos = strpos($url, $searchURL);
if ($pos === false) {
return false;
} else {
return true;
}
}
// SET URL
$url = "<a href='http://tumblr.com/my_post' rel='nofollow'>This site</a>";
// CALL FUNC
$run_url = find_excluded_url($url, 'google.com');
if ($run_url == true)
echo "URL - " . $url . "<br>";
$run_url = find_excluded_url($url, 'tumblr.com');
if ($run_url == true)
echo "URL - " . $url . "<br>";
Related
I am trying to write a script with PHP where it'll open up a text file ./urls.txt and check each domain for a specific word. I am really new to PHP.
Example:
Look for the word "Hello" in the following domains.
List:
Domain1.LTD
Domain2.LTD
Domain3.LTD
and just simply print out domain name + valid/invalid.
<?PHP
$link = "http://yahoo.com"; //not sure how to loop to read each line from a file.
$linkcontents = file_get_contents($link);
$needle = "Hello";
if (strpos($linkcontents, $needle) == false) {
echo "Valid";
} else {
echo "Invalid";
}
?>
$arrayOfLinks = array(
"http://example.com/file.txt",
"https://www.example-site-2.com/files/file.txt"
);
$needle = "Hello";
foreach($arrayOfLinks as $link){ // loop through the array
$linkcontents = file_get_contents($link);
if (stripos($linkcontents , $needle) !== false) { // stripos is case-insensitive search
// the needle exists in $linkcontents
// !== false instead of != false since stripos can return 0 meaning the needle is the first word of the contents
echo "Valid";
} else {
// the word does not exist in the given text
echo "Invalid";
}
}
First of all use CURL in favor of file_get_contents() because of security.
This would be a correct strpos example:
//your previous code
if (strpos($linkcontents, $needle) !== false) { // see the !==
echo "Valid"; //Needle found
} else {
echo "Invalid"; //Needle not found
}
For more complex crawling you could use some regexp instead of strpos.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
RegEx pattern to get the YouTube video ID from any YouTube URL
I have stored youtube url in the database.I want to fetch only youtube id from the youtube url.I just want to extract id(6FjfewWAGdE) from below url.
$youtubeVal=http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage
you can do this by
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match&&match[2].length==11){
return match[2];
}
Youtube URLs come in a variety of formats (with the embed/<id> thing or with the watch?v=<id>). Find the URL type(s) you want to understand and build regular expressions to extract them correctly.
Here is a working non-regexp solution in PHP:
function GetYoutubeID($url) {
$temp = parse_url($url);
if(isset($temp['query'])) {
parse_str($temp['query'], $temp2);
if(isset($temp2['v'])) {
return $temp2['v'];
}
}
if(isset($temp['path'])) {
$temp2 = explode("/", $temp['path']);
foreach($temp2 as $value) {
if(strlen($value) == 11 || strlen($value) == 10) {
return $value;
}
}
}
return "no ID?";
}
echo $youtubeID = GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage') . "\n";
echo $youtubeID = GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage') . "\n";
echo $youtubeID = GetYoutubeID('www.youtu.be/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('youtu.be/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be') . "\n";
Replace the whole content of the function with the following to use a more better looking solution (Regular expression):
preg_match('/^(http:\/\/|https:\/\/|.*?)(www.|.*?)(youtube.com|youtu.be)\/(embed\/|watch\?v=|.*?)(.*?)(\?|\&|$)/is', $url, $matches);
if(isset($matches[5])) {
return $matches[5];
}
return "no ID?"
The regular expression solution is also applicable on Javascript:
function GetYoutubeID(url) {
var regExp = /^(http:\/\/|https:\/\/|.*?)(www.|.*?)(youtube.com|youtu.be)\/(embed\/|watch\?v=|.*?)(.*?)(\?|\&|$)/;
var match = url.match(regExp);
if (match){
return match[5];
}
return "no ID?";
}
console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage'));
console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage'));
console.log(GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE'));
console.log(GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE'));
console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE'));
console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage'));
console.log(GetYoutubeID('www.youtu.be/6FjfewWAGdE'));
console.log(GetYoutubeID('youtu.be/6FjfewWAGdE'));
console.log(GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE'));
console.log(GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be'));
You can always try with
youtubeVal.substring(29, 39);
I am trying to search through a URL for a matching string, but the below code snippet doesn't seem to work.
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file = file($url);
if (in_array($search,$file)) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
If you are just searching for an occurrence of a string on the page, you can use
$str = file_get_contents($url);
if (strpos($str, $search) !== false) {
echo 'Success!';
} else {
echo 'Fail';
}
in_array() checks if an array member is equal to your needle.
It is improbable many websites will have a line which is equal to a only.
Also, is allow_url_fopen enabled?
That code will only find a line that has the exact $search string (likely including whitespace). If you're parsing HTML, check PHP's DOMDocument classes. Or, you can use a regex to pull what you need.
As #alex says, check is allow_url_fopen is enabled.
Also you can use strpos to search the string:
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file_content = file_get_contents($url);
if (strpos($file_content, $search) !== false) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
When someone writes:
"Near Tokyo"
I would like to check first if the $search contains "near" and if it does then take the "Tokyo" into a variable $location.
I tried this:
if(strpos($search, 'near') == true){
$search = explode("near ", $location);
echo $location;
exit();
}
did not work, it does not execute the if statement
You have multiple bugs here:
strpos may return 0, which signifies a match but will not compare equal to true
strpos is case-sensitive, which would make your example not work (look into stripos instead)
explode is also case-sensitive
It would probably be easiest to use a regex for this:
$input = "Near Tokyo";
if (preg_match('/near\s+(\w+)/i', $input, $matches)) {
echo "Near: ".$matches[1]."\n";
}
else {
echo "No match.\n";
}
See it in action.
This particular regex will only match the next word after "near", but this can be modified to suit your requirements.
returntype of strpos is int, not bool
http://php.net/manual/en/function.strpos.php
so use (according to manual pages) this:
if(strpos($search, 'near') !== false)
change it to:
if(strpos($search, 'near') !== false){
$search = explode("near ", $location);
echo $location;
exit();
}
just take a look at the documentation where this behaviour is explained:
It is easy to mistake the return values for "character found at
position 0" and "character not found". Here's how to detect the
difference:
<?php
$pos = strrpos($mystring, "b");
if ($pos === false) { // note: three equal signs
// not found...
}
?>
Yes, strpos, cumbersome boolean result handling. You probably should or should want to use stristr instead, which is also case-insensitive:
if (stristr($search, "Near")) {
And since you are extracting text anyway, why not use a regex? (People are using the awful explode workaround way too often.)
if (preg_match("'Near (\S+)'i", $search, $match)) {
echo $match[1];
}
use this to get a result:
if(strpos($search, 'near') !== false){
$location = explode("near ", $search);
print_r($location);
exit();
}
$search = "Near Tokyo";
if(strpos($search, 'Near') === 0){
$location = explode("Near ", $search);
echo $location[1];
exit();
}
<?php
$search = "Near Tokoyo";
if(preg_match("/near ([a-z]+)/i", $search, $match))
{
$location = $match[1];
echo $location;
}
?>
EDIT:
Fixed some bugs in your code.
if(stripos($search, 'near') !== false){
$location = explode("near ", $search);
echo $location[0];
exit();
}
I was wondering how I could check a string broken into an array against a preg_match to see if it started with www. I already have one that check for http://www.
function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
$stringToArray = explode(" ",$_POST['text']);
foreach($stringToArray as $key=>$val){
$urlvalid = isValidURL($val);
if($urlvalid){
$_SESSION["messages"][] = "NO URLS ALLOWED!";
header("Location: http://www.domain.com/post/id/".$_POST['postID']);
exit();
}
}
Thanks!
Stefan
You want something like:
%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i
this is using the | to match either http:// or www at the beginning. I changed the delimiter to % to avoid clashing with the |
John Gruber of Daring Fireball has posted a very comprehensive regex for all types of URLs that may be of interest. You can find it here:
http://daringfireball.net/2010/07/improved_regex_for_matching_urls
I explode the string at first as the url might be half way through it e.g. hello how are you www.google.com
Explode the string and use a foreach statement.
Eg:
$string = "hello how are you www.google.com";
$string = explode(" ", $string);
foreach ($string as $word){
if ( (strpos($word, "http://") === 0) || (strpos($word, "www.") === 0) ){
// Code you want to excute if string is a link
}
}
Note you have to use the === operator because strpos can return, will return a 0 which will appear to be false.
I used this below which allows you to detect url's anywhere in a string. For my particular application it's a contact form to combat spam so no url's are allowed. Works very well.
Link to resource: https://css-tricks.com/snippets/php/find-urls-in-text-make-links/
My implementation;
<?php
// Validate message
if(isset($_POST['message']) && $_POST['message'] == 'Include your order number here if relevant...') {
$messageError = "Required";
} else {
$message = test_input($_POST["message"]);
}
if (strlen($message) > 1000) {
$messageError = "1000 chars max";
}
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if (preg_match($reg_exUrl, $message)) {
$messageError = "Url's not allowed";
}
// Validate data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Try implode($myarray, '').strstr("www.")==0. That implodes your array into one string, then checks whether www. is at the beginning of the string (index 0).