How to extract the value in double quotation? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to extract the value of the attributes of a html tag that is defined as a php variable:
[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]
values ​​of src and poster should be separated and cast to array

if your text is a php variable, you can use preg_match :
<?php
$text = '[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]';
preg_match('/src=\"([^\"]+)\"/si', $text, $src);
preg_match('/poster=\"([^\"]+)\"/si', $text, $poster);
$array['src'] = $src[1];
$array['poster'] = $poster[1];
print_r($array);
?>
also you can use simplehtmldom
You can use an html parser for this. See this one: http://simplehtmldom.sourceforge.net/.
$html = str_get_html('<video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"></video>');
foreach($html->find('video') as $element)
{
$array['src'] = $element->src;
$array['poster'] = $element->poster;
}

Related

Get content from remote html page [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a way to get specific content from a remote web page
The content I want to get are inside javascript variables, this kind :
var Example1 = 0;
var Example2 = 14;
The name of the variable remain the same and the content is only numbers
Thank you
Find scripts in html source by DomDocument and then variable declaration by regex
$DOM = new DomDocument();
$DOM->loadHTML( $output);
$res = [];
$scripts = $DOM->getElementsByTagName('script');
$lnt = $scripts->length;
for($i=0; $i < $lnt; $i++) {
preg_match_all('/var\s+(\w+)\s*=\s*(\d+)\s*;/', $DOM->saveHtml($scripts->item($i)), $m);
$res = array_merge($res, array_combine($m[1], $m[2]));
}
print_r($res);
demo

How to match div content which has divs inside by using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Content is
<html>...<div id="endText" class="end-text" jcid="8311"><p>Hello</p>World<div class="ep-source cDGray"></div></div>...</html>
How to match
<p>Hello</p>World<div class="ep-source cDGray"></div>
Thank you!
#Rizier123
$content = '';
if(preg_match('/"endText".+?>.+?(?=<div.+?class="ep-source cDGray">)/i', $html, $contents) &&
preg_match('/(?<=>).+/i', $contents[0], $contentss))
{
$content = iconv('GBK', 'UTF-8', $contentss[0]);
return rtrim('OK' . "\t" . $content);
}
else
{
return rtrim('SKIP' . "\t" . 'NO_CONTENT');
}
This method can be used temporary, can't solve the problem.
Just match and remove the first-level div.
Regex (matches the opening div, saves its innards, and matches the last closing div):
/^<div id="endText"[^>]+>(.*?)<\/div>$/ism
PHP example:
preg_match('/^<div id="endText"[^>]+>(.*?)<\/div>$/ism', $html, $contents);
echo $contents[1];
// returns: <p>Hello</p>World<div class="ep-source cDGray"></div>
Adding the id attribute to the regex helps to specify that particular div

How can I get input fields names from a string with regex? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have some form in a variable. I have some input fields with names. How can I get all name="" from that string?
Now I'm trying this:
preg_match_all ('/(name="(.*?"))\d/s', $ki, $matches1);
But not working well. It gives me just a few and it gives me a lots of unnececary informations.
Try with this regex:
preg_match_all( '/name\s*=\s*(?|"(.*?)"|\'(.*?)\')/', $ki, $matches1 );
You can also do this using array.
i.e. By using explode method
$content='name="name1" other example text name="name2" other example text name="name3" ';
$finalResult=array();
function getBetweenAll($content,$start,$end)
{
$res = explode($start , $content , 2);
if(isset($res[1]) && $res[1]!='' )
{
$value = explode($end , $res[1] , 2);
global $finalResult;
$finalResult[]=$value[0];
if(isset($value[1]) && $value[1]!='')
{
$content=$value[1];
getBetweenAll($content,$start,$end);
}
}
}
$start='name="';
$end='"';
getBetweenAll($content,$start,$end);
echo "<pre>";
print_r($finalResult);
echo "</pre>";

"\u00e1n" to "á" in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am receiving an external file with strings, I have no control over that file but I receive special characters as this (i think it's unicode?) sequence \u00e1n.
Is there anyway to convert this kind of char sequences to their "readable" counterparts?
Thanks!
EDIT:
I am calling a url that gives me a list with names of persons:
Tom\u00e1nsson\n
Eriksen\n
Gilverto\n
I am reading the names and showing them in my site.
EDIT
Now, I've figured out the correct, working answer based on the article I found. Here is it:
function unicode2utf8($str) {
$a=json_decode(str_replace("\n","<br>",'["'.$str.'"]'));
return str_replace("<br>","\n",$a[0]);
}
PHP:
echo unicode2utf8("Tom\u00e1nsson\n Eriksen\n Gilverto\n");
Output:
Tománsson
Eriksen
Gilverto
ORIGINAL
I've found an article about the same problem here ( http://www.welefen.com/php-unicode-to-utf8.html )
The solution is the following function
function unicode2utf8($str){
if(!$str) return $str;
$decode = json_decode($str);
if($decode) return $decode;
$str = '["' . $str . '"]';
$decode = json_decode($str);
if(count($decode) == 1){
return $decode[0];
}
return $str;
}
I've tried it with
echo unicode2utf8("\u00e1");
Output:
á

Assign text between tags to an variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to get the text between <p></p> tags and set this in a variable?
<p>blabla</p> So i would like to get the text "blabla" and set this into a php variable so the variable would have the text value like this:.
<?$test = blabla;?>
Try:
$html = "<p>blabla</p>";
$dom = new DOMDocument;
$dom->loadXML($html);
$arr = $dom->getElementsByTagName('p');
foreach ($arr as $value) {
echo $value->nodeValue; // result => blabla
}
There are many methods which can be used based on your needs so take a look on documentation
DOMDocument
You can use this function, it is self explanatory:
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname>(.*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
?>

Categories