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']);
Related
This question already has an answer here:
Remove comments from JSON data
(1 answer)
Closed 4 years ago.
In a task I had to write comments in JSON files and decode them using PHP. I share it. Maybe it helps someone else. The regex was tough.
Remove the comments with regular expression
$jsonStringWithComments = file_get_contents ( "jsonFileWithComments.json" );
$pattern = '/(((?<!http:|https:)\/\/.*|(\/\*)([\S\s]*?)(\*\/)))/im';
$decodedObject = json_decode ( preg_replace ( $pattern, '', $jsonStringWithComments ) );
This regular expression can capture "one-line" comments, start double slashes // or "multi-line" comments between slash asterisk /* and asterisk slash */.
With it, you can parse JSON like the following:
{
"lorem": "ipsum",
"dolor":/* inline comment */ "sit amet",
"consectetur": "adipiscing",
/*
multi
line
comment
*/
"Pellentesque": "ultrices",
//single line comment
"lectus": "nec",
"elit": "dictum",
"url": "http://example.com"
}
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:
Insert string at specified position
(11 answers)
separate string in two by given position
(8 answers)
Closed 4 years ago.
I want to explode a string or an integer and separate it by a space.
E.g., I have this int 12345678, and I want its numbers to become like 123 45678. I want the first three numbers separated. Can someone give me a clue or hint in how to achieve this, like what function to use in PHP? I think using an explode will not work here because the explode function needs a separator.
You can use substr_replace() - Replace text within a portion of a string.
echo substr_replace(1234567, " ", 3, 0); // 123 4567
https://3v4l.org/9CFlX
You could use substr() :
$str = "12345678" ;
echo substr($str,0,3)." ".substr($str, 3); // "123 45678"
Also works with an integer :
$int = 12345678 ;
echo substr($int,0,3)." ".substr($int, 3); // "123 45678"
This problem will solve by using substr().
The substr() function returns a part of a string.
Syntax: substr(string,start,length)
Example:
$value = "12345678";
echo substr($value,0,3)." ".substr($value, 3);
Output: 123 45678
You may get better understand from here.
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";
}
}
This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);