Why won't grep functions work on localhost? - php

$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!";
}

Related

'Array' showing in my foreach loop

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;
}

How to use wildcards in strpos

I have the below script which is working. I am looking for the string '1/72'
I would also like to look for the string '1:72' and '1\72' - how would I go about doing this easily (apart from just using multiple if statements?)
$string="test string 1/72 PHP";
if (strpos($string, '1/72') > 0) {
print "Got match!\n";
} else {
print "no match\n";
}
Use preg_match function.
if (preg_match("~1[\\\\:/]72~", $str)) {
[\\\\:/] character class which matches a backslash or : or forward slash.
DEMO
The fnmatch() function also provides a simple way to use pattern matching with shell wildcard expressions
$string="test string 1/72 PHP";
if (fnmatch('*1[/:\\\\]72*', $string)) {
print "Got match!\n";
} else {
print "no match\n";
}

PHP preg_match? How to return not matched characters?

Lets say i have:
$string = 'qwe1ASD#';
if(preg_match('/^[a-zA-Z]+$/', $string))
{
echo 'OK';
}
else
{
echo 'BAD';
}
Now, is there simple solution, to find all characters from $string which don't match expression? So in return, in place of "BAD" i want to have ex. "BAD. You can't use following characters: 1#"
Thanks in advance for any simple hints! :)
Thank you Floern, your answer suit best my needs. It have only one "preg" so it's also good for performance. Thank you again.
I implemented it for now as follw:
if(preg_match_all('/[^a-zA-Z0-9]/s', $string, $forbidden))
{
$forbidden = implode('', array_unique($forbidden[0]));
echo 'BAD. Your string contains forbidden characters: '.htmlentities($forbidden).'';
}
$tmpstring=preg_replace('~[A-Za-z]~','',$string);
if(strlen($tmpstring))
//bad chars: $tmpstring
You could use preg_match_all():
if(preg_match_all('/[^a-zA-Z]/s', $string, $matches)){
var_dump($matches); // BAD
}
else{
echo 'OK';
}
$string = 'qwe1ASD#';
if(preg_match('/^[a-zA-Z]+$/', $string))
{
echo 'OK';
}
else
{
echo 'BAD. You cannot use the following characters: ' + preg_replace('/[a-zA-Z]/', '', $string);
}
There are different ways. I find this one nice:
$check = preg_prelace('/[a-zA-Z]/', '', $string);
if ($check) echo 'BAD ' . $check;
UPDATE:
if (strlen($check)) echo 'BAD ' . $check;

preg_match for information in parentheses

(ACCTG) Accounting
The text above I trying to get the information in the parentheses how would I do that in php this is what I have so far.
$regex = '#\((([^()]+|(?R))*)\)#';
if (preg_match_all($regex, $string ,$matches)) {
echo implode(' ', $matches[1]);
} else {
//no parenthesis
echo $string;
}
I do converting special characters to hex for easy use in my regex's
<?
$input = 'abc ("an example")';
if(preg_match("/\x28([^\x29]+)\x29/", $input, $matched)) {
//...
print_r($matched);
} else {
//do something..
}
?>
Wouldn't this be sufficient:
\(([^\)]*)\).*

Error in preg_match PHP

<?php
$str = "asd,ad";
if(preg_match(",",$str)) {
echo "ok";
}
?
It outputs me
No ending delimiter ',' found in....
?>
your pattern can be replaced to strpos instead
if(strpos($str, ",")!==false)
{
echo "ok";
}
You are missing delimitters, try this:
$str = "asd,ad";
if(preg_match("/,/",$str)) {
echo "ok";
}
To find out more instances, you may want to use preg_match_all function too.

Categories