Simple_html_dom - Find first line only - php

I use Simple_html_dom and I want it to show the first span.muted it finds on the page.
foreach($html->find('span.muted') as $e)
echo $e->innertext . "<br>";
Currently it execute a list of all the span.muted, and can't really seems to make it work like I want it.
This is what it looks like when i use the code.
3.1
Version: 3.1
Version: 3.1
Version: 3.1
And only want it to show 3.1 in this example.

If you want to show only the first one it finds without modify much of your code. Maybe you can use break like this.
foreach($html->find('span.muted') as $e) {
echo $e->innertext . "<br>";
break;
}
Or you can simplify it to:
echo $html->find('span.muted')->innertext . "<br>";

If the result is accurate and you're looking to grab only the version number regardless of the text that precedes it, you could use regular expressions on the result:
foreach($html->find('span.muted') as $e)
if(preg_match('/\d+\.\d+$/', $e->innertext, $matches)) {
echo $matches[0] . '<br />';
}
}

It looks like this:
echo $html->find('span.muted', 0)->innertext;

Related

How do I print and store regex (Regular Expressions) in PHP and MYSQL

I need to be able to store and echo regular expressions. In my case the user enters the regex into a form and that exact sequence of characters needs to be echo-ed to the screen sometime later. The problem is that the echo changes the characters.
So for instance I have tried this
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
but when I echo it..
echo $regex;
I get...
((((amount\">\$)(.*?)(?=)
If I do this
$regex = htmlentities($regex);
I get this which helped with the missing part of the regex but not the multiple ((((
((((amount\">\$)(.*?)(?=</strong>
htmlspecialchars did not help either.
How do I get it to echo the variable exactly as it is written? And what would I need to do to store them in MySQL and retrieve them exactly as written?
EDIT - in response to some observations below, I add a bit more detail. This new example was done on a PHP 7.1 server in the cloud, Centos 7 rendered using Chrome.
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
$page_elements_regex[1][0] = $regex;
$page_elements_regex[1][1] = addslashes($regex);
$page_elements_regex[1][2] = htmlspecialchars($regex);
$page_elements_regex[1][3] = htmlentities($regex);
echo "regex " . $page_elements_regex[1][0] . "<BR>";
echo "addslashes " . $page_elements_regex[1][1] . "<BR>";
echo "htmlspecialcharacters " . $page_elements_regex[1][2] . "<BR>";
echo "htmlentities " . $page_elements_regex[1][3] . "<BR>";
Results
regex ((((amount\">\$)(.*?)(?=)
addslashes ((((amount\\\">\\$)(.*?)(?=)
htmlspecialcharacters ((((amount\">\$)(.*?)(?=</strong>)
htmlentities ((((amount\">\$)(.*?)(?=</strong>)
It is also a big clue that if you take off the first ( like this
$regex = '?<=amount\">\$)(.*?)(?=</strong>)';
The result removes the first a of amount!! Is it interpreting the regex instead of echoing it?
?(((mount\">\$)(.*?)(?=)
I have solved it, and I feel a bit foolish about the answer. My bad.
Somewhere else in my code I had this
$regex[1] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[2] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[3] = '(?<=amount\">\$)(.*?)(?=</strong>)';
I have no idea why this gave the result it did, rather than a straight up error, but once removed it all is fine. The bottom line is that both htmlspecialcharacters and htmlentities give the right answer, Lesson learnt. Check all the code, my mistake was in the use of arrays, defining $regex as an array and a variable, not as I first thought here.

How to create wordpress function?

i need to create function in word press function file to print some HTML cord. i tried this function.
function pre($chord){
echo '<pre>$chord</pre>'
}
and i use it in my post like this
<?php pre("sd");?>
and it will not work. please help me.
You are building your string incorrectly. when mixing html and php strings you need to concatenate the data like so...
function pre( $chord ) {
echo '<pre>' . $chord . '</pre>';
}
Also bear in mind that Wordpress is a large and modular CMS so by naming your function something as simple as pre you run the risk of conflicting with some other function called pre. It's good practise to prefix your functions with a unique string of letters like so...
function abc_pre( $chord ) {
echo '<pre>' . $chord . '</pre>';
}
Use whatever works for you.
Dan

PHP OOP confusion for noobie

I have a problem that's born of being a dyed in the wool procedural programmer who's here forced into using some OOP constructs in order to make use of a library I need to use. I am stuck unable to access variables- other than print them out. Let me illustrate:
foreach($html->find('span[class="given-name"]') as $e)
echo $e->innertext . '<br>';
foreach($html->find('span[class="family-name"]') as $e)
echo $e->innertext . '<br>';
The above will print out a long list of first names, followed by a long list of surnames. However I want to be able to access them together. For example I want to be able to say something like $myarray["firstname"] = (*whatever is in $e->innertext*) and also $myarray["surnamename"] = (*whatever is in the next $e->innertext*)
When I try the seemingly obvious:
$x = $e->innertext;
it crashes. I assume that's because I am passing a pointer to $x instead of a value, but how on earth do I tell it I want the value - in this case a part of a person's name, to be assigned to $x, or my array, or whatever the variable might be?
I am almost a complete neophyte when it comes to OOP concepts and constructs, so please bear that in mind. Thank you for your assistance!
If your document is well structured and in order, this should do the trick:
$name=array();
$surname=array();
foreach($html->find('span[class="given-name"]') as $e){
$name[]=$e->innertext;
}
foreach($html->find('span[class="family-name"]') as $e){
$surname[]=$e->innertext;
}
foreach($name as $key=>$value){
echo $name[$key] . " " . $surname[$key] . "<br>";
}

How to get difference between two words in php

I have a search engine for my website. I have installed a feature like Google's "Did you mean xxx?". It returns corrected string. So, is there a way so that I can compare both of those incorrect and correct strings and bold out the corrected words like Google do?
Here is my code:
<?php
function suggestion($word){
$first_word=substr($word,0,1);
$first_word=strtoupper($first_word);
$query="SELECT * FROM words WHERE UPPER(LEFT(name,1))='$first_word'";
$db_words=mysql_query($query);
$flag=true;
while($row_words=mysql_fetch_array($db_words)){
similar_text($row_words['name'],$word,$percent);
if(($percent>50)&&($percent!=100)){
echo '<strong>Did you mean:</strong><br>';
echo $row_words['name'],'<br>';
$flag=false;
}
}
}
?>
According to the comments below you are looking for something like this:
function corrected($bad, $good, $wholephrase) {
return preg_replace('#\b'. preg_quote($bad) . '\b#g', '<em>' . htmlspecialchars($good) . '</em>', $wholephrase);
}
For those of you who were interested in how the detection of misspelling and suggestion of alternatives is accomplished take a look here: Levenshtein Distance
No one helped me!! ;-(
Here is the code I created:
function suggestion($word){
$first_word=substr($word,0,1);
$first_word=strtoupper($first_word);
$query="SELECT * FROM words WHERE UPPER(LEFT(name,1))='$first_word'";
$db_words=mysql_query($query);
$wd=explode(" ",$word);
while($row_words=mysql_fetch_array($db_words)){
similar_text($row_words['name'],$word,$percent);
if(($percent>50)&&($percent!=100)){
$rw=explode(" ",$row_words['name']);
$d=array_udiff($rw,$wd,'strcasecmp');
for($i=0;$i<count($rw);$i++){
if(in_array($rw[$i],$d)){
$rw[$i]="<em>".$rw[$i]."</em>";
}
}
echo '<strong>Did you mean:</strong><br>';
foreach($rw as $r){
echo $r," ";
}
echo '<br>';
}
}
}

Modified variable variables

$m = 'this_is_m';
$this_is_m = 'this_is_m_not_full :(';
$this_is_m_full = 'this_is_m_FULL!! :D';
print ${$m};
(Hi first :P) Outputs:
this_is_m_not full :(
Any idea how to output this_is_m_FULL!! :D using $m??
What I've already tried:
print $"{$m}_full";
print $'{$m}_full';
print $'$m_full';
print ${$m}_full';
None worked... Thanks in advance,,
The solution would be:
print ${$m . '_full'};
But that feels very hackish.
To get your desired output, you need to do the following:
print ${$m . "_full"};
The following should work:
print ${$m.'_full'};
This is because the string inside the braces will get evaluated first, becoming
print ${'this_is_m' . '_full'}
-> print ${'this_is_m_full'}
-> print $this_is_m_full
Take a look at this manual page if you want more information on this.

Categories