This question already has answers here:
Extract HTML attributes in PHP with regex [duplicate]
(2 answers)
Closed 7 years ago.
i have the following html structure :
<div data-provincia="mi" data-nazione="it" ecc...
I'm trying to take "mi" with preg_match function.
This is my code :
$pattern = '/data-provincia=*"[a-zA-Z]*"/';
preg_match($pattern,$element,$provincia);
I think that the code is right but it doesn't match with anything.
Where i'm wrong?
Thanks.
You might want to use the quantifier + (1 or more) next to the character class between brackets, and remove the first star. Also added a subtpattern for you to get exactly the part you want. Give it a try :
$pattern = '/data-provincia="([a-zA-Z]+)"/';
preg_match($pattern,$element,$provincia);
echo $provincia[1];
$element = '<div data-provincia="mi" data-nazione="it" ecc...>';
$pattern = '/<div[^>]*data-provincia=\"([^\"]+)\"[^>]*>/';
preg_match($pattern,$element,$provincia);
print_r($provincia[1]);
In addition to my comment, for this simple attribute you can use the following regex:
$regex = '/data-provincia="([^"]*)/i';
preg_match($regex,$element,$matches);
echo $matches[1];
Basically match everything except a double quote as many times as possible (or none). But please at least consider using a Parser for this task, regular expressions were not meant to deal with it.
Its working fine for me
$element = '<div data-provincia="mi" data-nazione="it"></div>';
$pattern = '/data-provincia=*"[a-zA-Z]*"/';
$matches= array();
preg_match($pattern,$element, $matches);
if (!empty($matches)) {
foreach ($matches as $eachOne) {
//code to remove unwanted
$text = trim(preg_replace('/^data-provincia\=/', '', $eachOne), '""');
echo " $eachOne; $text";
}
}
Related
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 4 years ago.
I get the content from an URL: http://money18.on.cc/js/real/hk/quote/16981_r.js
The content of this file is something like this:
M18.r_16981 = {
"ltt": '2018/06/21 11:43',
"np": '0.050',
"iep": '0.000',
"iev": '0',
"ltp": '0.050',
"vol": '940000',
"tvr": '49860',
"dyh": '0.060',
"dyl": '0.049'
};
I want to extract the number after "vol", i.e. 940000.
And this is my php:
<?php
$content = file_get_contents("http://money18.on.cc/js/real/hk/quote/16981_r.js");
echo $content."<br>";
preg_match("/(?<=vol\": ').*(?=', \"tvr)/", $content, $output_array);
print_r(array_values($output_array));
?>
The result returns nothing. Is the Regex wrong or any other problem?
Thanks.
In your positive lookahead (?= you could replace the whitespace with \s* to match zero or more (or \s+ to match one or more) whitespace characters.
(?<=vol": ').*(?=',\s*"tvr)
Regex demo
Php demo
Bit of a long shot but...
If that content is actually meant to represent JSON and you have control over it:
get it into proper format as it is much easier and secure to get the data you want out of it then.
$jsonString = <<<JSON
{
"ltt": "2018/06/21 11:43",
"np": "0.050",
"iep": "0.000",
"iev": "0",
"ltp": "0.050",
"vol": "940000",
"tvr": "49860",
"dyh": "0.060",
"dyl": "0.049"
}
JSON;
$jsonAsArray = json_decode($jsonString, true);
var_dump($jsonAsArray['vol']);
This question already has answers here:
removing #email.com from string in php
(6 answers)
Closed 4 years ago.
How can I use the str_replace() without using an array of words for turning:
some#mail.com
into
some
So, everything after the '#' sign includes # as well.
How can I do that?
As an example, i will type 'adminofsite#xxxwwweeerandomstuff.com'
and the output will be: 'adminofsite'.
use strstr
$email="john#doe.com"
$user = strstr($email, '#', true);
echo $user;
$str = "some#mail.com"
$str = substr($str,0,strpos($str,"#"))
function stripEmailDomain($string){
return substr($string, 0, strpos($string, '#'));
}
I exploded and then rebuilt the string. This will work if you are positive the string will always be an email address. Its a work around but seems flexible if you want to modify it for a specific purpose.
<?php
$explo0= explode('#',"some#mail.com");
for($i=0;$i<count($explo0);$i++){
$exploresult0.=$explo0[$i+1];
}
echo "#".$exploresult0;
?>
This question already has answers here:
Search in array with relevance
(5 answers)
Closed 9 years ago.
I am trying to search an array for a list of words(areas).
But sometime the word(area) in the array is 2 words.
i.e in the array is "Milton Keynes" so "Milton" is not being matched
Is there any way i can do this, without splitting any double words in the array (as i assume this will be a big load on the server)
Below is an example of what i am doing
foreach (preg_split("/(\s)|(\/)|(\W)/", $words) as $word){
if (in_array($word, $areaArray)){
$AreaID[] = array_search($word, $areaArray);
}
}
Grateful, as always for any advice!
You could use preg_grep():
$re = sprintf('/\b%s\b/', preg_quote($search, '/'));
// ...
if (preg_grep($re, $areaArray)) {
// we have a match
}
You can opt to make the match case insensitive by adding the /i modifier.
You can use regular expression to find a value, this will work similar to MySQL like function
$search='Milton Keynes';
foreach ($areaArray as $key => $value) {
if (preg_match('~'.preg_quote($search).'~i',$value)) {
echo "$key";
}
}
This question already has answers here:
Remove resolution string from image url in PHP
(4 answers)
Closed 9 years ago.
Regular expressions are a bit of a challenge for me. My goal is to determine whether or not a filename ends with this:
_100x200.jpg
where 100 and 200 could be any integer of any number of digits.
So what I'm looking for is a way to match these filenames:
photo_1x3.jpg
abc_100x100.jpg
a file name_50x2000.jpg
Could anybody help me out?
Thanks!
You can use this regex:
/_\d+x\d+\.jpg$/i
Using inside your code you can do:
if (preg_match('/_\d+x\d+\.jpg$/', $image, $arr)) {
// matched
var_dump($arr);
}
You can use preg_match to both check and get your dimensions:
<?php
$str = array(
'photo_1x3.jpg',
'abc_100x100.jpg',
'a file name_50x2000.jpg'
);
foreach($str as $s) {
$match = preg_match( '/_([0-9]+)x([0-9]+)/', $s, $matches);
echo $match ? 'exists' : 'doesn\'t exist';
print_r($matches);
}
?>
Demo: https://eval.in/65623
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP preg_match_all: Extract comma seperated list
I have a string that looks like: [something ="1,2,3"]
I need get everything between the quotes into a string. I'm sure that preg_match is the way to do this but i'm not sure of the expression to use.
$content = [something = "1,2,3"];
$new_string = preg_match('?regex?','$content');
Try this:
$content = '[something = "1,2,3"]';
if (preg_match('/"([^"]+)"/', $content, $matches)) {
$matchedContent = $matches[1];
}