PHP find function without found element html [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to figure out how to grab the string inside H1 without actually H1 inside the string.
$html = "<div id="test"><h1>hello world</h1></div>"
foreach($html->find('div[id=test] h1') as $test1){
echo '<div>';
echo $test1;
echo '</div>';
}
$test1 returns "<h1>hello world</h1>"
I'd like for $test1 to be just "hello world" and without the h1 tags. Thank you in advance

You can use regex for this:
$str = '<div id="test"><h1>hello world</h1></div>';
$matches = array();
preg_match('/<h1>(.*)<\/h1>/', $str, $matches);
echo $matches[1];
Outputs: hello world
Docs:
http://www.php.net/preg_match
Demos:
https://eval.in/78867
http://regex101.com/r/lG3dO8

Related

preg match for known value plus 7 charecters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone help me get the value of IFC/USD 0.00001031 from the webpage http://infinitecoin.com.
I need to grab the value after IFC/USD..
The way I thought to do it was to:
$file_string = file_get_contents('http://infinitecoin.com');
preg_match('/IFC/USD plus next 11 charecters', $file_string, $title);
$value = $title[1];
echo $title_out ;
But im not sure how to ask PHP to find IFC/USD then return the next 11 characters after that.
If I could accomplish this, my task would be solved..
Any help would be great.
Thank you
Jason
$output = array();
preg_match("/(?<=IFC\/USD\s)[\d\.]+/", 'IFC/USD 0.00001015', $output);
$output will look like this:
Array
(
[0] => 0.00001015
)

I want to grab href address which has class link [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
[a href="http://www.vanchosun.com/m/market_index.php?view=market"]11[/a]
[a href="test.php?view=test&cate=buysell&testid=100" class="link"]22[/a]
How to grab href which has class link using php preg_match_all
Thanks.
You can use this:
$pattern = '~(?(DEFINE)(?<class>\bclass\s*=\s*"[^"]*?\blink\b[^"]*"))
<a\s+ [^>]*?
(?| \g<class> [^>]*? \bhref\s*=\s*"([^"]*)"
| \bhref\s*=\s*"([^"]*)" [^>]*? \g<class>)~xi';
preg_match_all($pattern, $code, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
echo '<br/>' . $match[1];
}
However peiman F. has a good answer, since the DOM is a better choice for this kind of task.
A DOM way:
$doc = new DOMDocument();
#$doc->loadHTML($code);
$links = $doc->getElementsByTagName('a');
foreach ($links as $link) {
if (preg_match('~\blink\b~i', $link->getAttribute('class')))
echo '<br/>' . $link->getAttribute('href');
}
for jobs like this its better to use php DOM
see the easy how to use tutorial
use DOMElement::getAttribute method in all method list
http://www.php.net/manual/en/domelement.getattribute.php

Break string into fields [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string input using PHP that look like this:
Log,,t1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23
I want to save it into MySQL
field:reading
t1:12.12
t2:20.23
t3:30.00
t4:50.20
Temp:11.23
Can anyone give me an direction?
Begin you have to parse to rows and columns
$string = 't1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23';
$rows = explode(':', $string);
$table = array();
foreach(rows as $row) {
$table[] = explode(',', $row);
}
You can use regex below:
if (preg_match_all('/[a-z]+\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}
The regex can be a bit simpler if each field will called "t+number":
if (preg_match_all('/t\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}

Variable Not Working With Glob Function In Php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
foreach(glob($select)) as $filename){
echo $filename;
echo "<a class='vlightbox1' href='$filename' title='$filename'><img src='$filename' style='height:120px; width:160px; alt='$filename'></a>";
echo "<a href='ap_deleteimages.php?id=$filename'>Delete</a>";
}
It is not working Properly. I Have Added $select Value as under
$select=document.write(document.getElementById('flist').value)
and 'flist' is a option tag id in html and glob function do not work with it
foreach(glob($select) as $filename){
echo $filename;
echo "<a class='vlightbox1' href='$filename' title='$filename'><img src='$filename' style='height:120px; width:160px; alt='$filename'></a>";
echo "<a href='ap_deleteimages.php?id=$filename'>Delete</a>";
}
You have the syntax error at glob function in your code. The right syntax is in above code.
Second thing is that glob() is used to find all the search pathname matching a pattern. So you need to check that the value of $select is correct or not.
Read more above glob() function.
How about putting in the parentheses?
foreach ( glob($select) as $filename)

Trim string after and before a character [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to trim this string after "=" and before ";"
com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;
so that it echoes 8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75
You can use the str_replace function to replace multiple values by, in this case, nothing:
str_replace(array('com=', ';'), '', $string);
This can also work:
parse_str(trim(
'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;'
, ';'));
echo "$com\n";
Try this:
$value="aaaacom=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;";`
$pos=strpos($value,"=");
echo substr($value,$pos+1,strlen($value)-($pos+2));
You can use a regex:
$str = 'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;';
echo preg_replace('/([a-z]+)=(.*);/', '\2', $str);
But it looks like a .ini file, then maybe you want this:
Try this:
$ini = 'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;';
$parsed_ini = parse_ini_string($ini);
echo $parsed_ini['com'];

Categories