For some reason I'm getting the word 'array' as an output when I try to do a foreach to echo out the values in an array (there are 2 values). These show fine if I use print_r in the array so I know they are there. I've also tried using as list but that only shows the first value and nothing after it.
It's getting late so it might be something pretty silly! Thanks in advance
<?php
$crawl_url = "./emails.php";
function get_email($url) {
$input = #file_get_contents($url);
$regexp = '/[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i';
preg_match_all($regexp, $input, $matches);
if (empty($input)) {
echo "No email addresses found";
}
else {
foreach($matches as $matches_values) {
print $matches_values;
}
}
}
get_email($crawl_url);
echo '<br /> function complete';
?>
I think you're missing the 0 index on $matches.
Try this :
foreach($matches[0] as $matches_values) {
print $matches_values;
}
Related
The text between the quotes I am trying to extract, also has quotes within the text, like this: "data":"value"
The specific word is "data". The "value" will always be different. Once extracted it should be "data":"value" with the quotes.
So far...
<?
$dat = "file.dat";
$text = file_get_contents($dat);
$res = preg_match_all ('/"([^"]*)"/', $text, $matches);
if ($res) {
foreach (array_unique ($matches[0]) as $data) {
echo "$data";
}
}
else {
echo "No Data Found.";
}
My code just extracts any and all words between double quotes. How do I accomplish what I am trying to do? Thank you in advance.
As in the comments noticed. The format you are trying to parse is JSON and PHP comes with a easy to use function to parse JSON. You could try this:
<?
$dat = "file.dat";
$text = file_get_contents($dat);
$res = json_decode($text, true);
if ($res) {
foreach ($res as $key => $value) {
echo "$key => $value";
}
}
else {
echo "No Data Found.";
}
Untested, but should get the job done.
I am trying to identify a blank lines in a string. Below is my attempt in PHP:
<?php
$alldevs = $_POST['devs'];
$devices = explode("\n", $alldevs);
foreach($devices as $device) {
if(!empty($device)){
echo $device;
} else {
echo "end of value";
}
}
?>
When I input the following:
1
2
3
4
I get this output:
1
2
3
4
But what it should be outputting is this:
1
2
3
end of value
end of value
4
What am I doing wrong?
They probably contain a \r (which is posted on new lines in text areas for some browsers/OS'es), a space or a tab character. You can get rid of these by using the trim() command:
<?php
$alldevs = $_POST['devs'];
$devices = explode("\n", $alldevs);
foreach ($devices as $device) {
$device = trim($device); //Trim that string!
if(!empty($device))
{
echo $device;
}
else
{
echo "end of value";
}
}
?>
Oh, and PLEASE indent your code for your own and everybody elses sake.
Alternatively, split up your string by using regex:
$devices = preg_split("/(\r\n|\n\r|\r|\n)/", $alldevs);
This should give you what you want:
if( trim($device) !== '' )
{
echo $device."<br>";
}
else
{
echo "end of value"."<br>";
}
Outputs:
1
2
3
end of value
4
I think your problem is with \r\n
Use this code
$alldevs = str_replace("\r", '', $alldevs);
Then explode it, and also use trim for clean spaces
$alldevs = trim($alldevs);
first, please read dealing with line endings and wikipedia newline
second, you are using string explode when you should use a function like preg_match_all
code should look something like this (mind the bad regex please):
<?php
$string = $_POST['devs'];
preg_match_all('%^([^\n\r]*)[\n\r]?$%im', $string, $matches);
foreach ($matches[1] as $match) {
if($match) {
var_dump($match);
} else {
echo 'empty line' . PHP_EOL;
}
}
adjust this code to fit your needs, i left a var_dump there so you could see the string length.
Add a check for a string with more than 0 characters,
if(!empty($device) && strlen($device)>0) {
I would also try a use case with \r\n on your line-breaks, you'll run into that as well.
you can try this
$devices = preg_replace('/^\s*$/','end of value',explode("\n",$alldevs));
foreach($devices as $device) {
echo $device, "\n";
}
I have this kind of string format used in a preg_match:
[Day][MonthAbbr] [Date] [Hour]:[Minutes][AM/PM]
example:
ThuDec 27 2:00am
Having this $pattern and some sort of code:
$pattern = "/([A-Z]{2}\w+)([A-Z]{2}\w+)\s+(\w+)\s+(\d+):(\d+)(..)/ims";
$match = array();
if (preg_match($pattern, rtrim($date), $match)) {
echo '<pre>';
print_r($match);
echo '</pre>';
} else {
echo 'Could not parse date.';
}
i was able to extract the Month, Day, Time, etc... from the string..
But i was wondering why i came up with the catch 'Could not parse date.' even if the value being passed was still the same..
I compared the values and the one which has an error:
ThuDec 27 2:00am
ThuDec 27 4:30am (gives the error)
Here's the screen shot below to make a comparison:
Is there something wrong with the pattern ive been using in the preg_match?
<?php
$a = "ThuDec 27 4:30am";
$pattern = "/([a-z]{3})([a-z]{3})\s+(\d+)\s+(\d+):(\d+)([a-z]{2})/i";
$match = array();
if (preg_match($pattern, $a, $match)) {
echo '<pre>';
print_r($match);
echo '</pre>';
} else {
echo 'Could not parse date.';
}
?>
This code works pretty well for me.
$grep=preg_grep("/^(\d+)?\.\d+$/", "11.11");
print_r($grep);
Shouldn't this be printing an array consisting of "11.11"?
No...you want to use preg_match... preg_grep is for arrays:
if(preg_match("/^(\d+)?\.\d+$/", "11.11", $matches)) {
print_r($matches);
} else {
echo "No Match!";
}
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.";
}
?>