Extract number in Preg_match_all of jQuery - php

Just trying to extract the second td (which is a number) of the table that is, the value of 235. Values of numbers change dynamically.
jQuery('.content').append('<tr><td>Volume1</td><td>56</td><td>123</td></tr>');
jQuery('.content').append('<tr><td>Volume2</td><td>235</td><td>789</td></tr>');
In php I have this:
if(preg_match_all("/jQuery\(\'.content\'\).append\((\'<tr><td>Volume2</td><td>(.*?)\')\);/", $result))
Of course it does not work because I don't know how to use regex in html tags. Please help
Edit:
The solution must be in php because jQuery receive from another server using the curl php.

You can do:
$(".content tr:eq(1) td:eq(1)").text();
Demo: http://jsfiddle.net/zuhb9/
If you have multiple class content then you need to loop through all of them:
$(".content").each(function() {
console.log($(this).find("tr:eq(1) td:eq(1)").text());
});
Demo: http://jsfiddle.net/zuhb9/1/

Related

How to find all class names or ids from a CSS file that contains a background image using Regular expression and PHP?

I want to get the class name or the ID from a text using regular expression with PHP.
For Instance:
.vc_custom_1547091583528{
margin-bottom:40px!important;
padding-top:7rem!important;
padding-bottom:1rem!important;
background:#092746 url(http://icoachu.us/wp-
content/uploads/2016/12/princeton.jpg?id=957)!important;
background-position:center!important;
background-repeat:no-repeat!important;
background-size:cover!important
}
This is css and the output should be:
array(0=>".vc_custom_1547091583528");
and also
array(0 => "http://icoachu.us/wp-content/uploads/2016/12/princeton.jpg?id=957")
So they will be two different functions.
Finally I have found the right answer. The regular expression can be made more smarter and short but right now this one works just fine.
([.#][a-zA-Z0-9_-]*?){[a-zA-Z0-9~;:#! _-]*\(['"]?(.*?)['"]?\)[a-zA-Z0-9~;:#! _-]*}

Change values in dropdown when some characters are entered in textbox using ajax in php

I am reading csv file in php Having 2 columns in it. what i want is when i enter any character from first column from csv in text box then i want related values of second column from same csv file to be come in drop down list. i am quite new to use ajax not get any such type of example.
Can anybody help me?
Thanks..
Here's an example you're looking for: http://www.99points.info/2010/12/n-level-dynamic-loading-of-dropdowns-using-ajax-and-php/
Not quite sure what you want to do, but if you are using jquery, then ajax is very simple. You can use:
var sChar = 'c';
$.get('yourPhpFile.php', { sCharacter:sChar }, function(sData){
alert(sData);
});
This way you call yourPhpFile.php, sCharacter is available in yourPhpFile.php as $_GET parameter, and sData holds the output.
See http://api.jquery.com/jQuery.get/

Why isn't a .post and .html call able to compare in an if(condition) in jQuery?

The following script has been created to test if the value of a db field has changed and if so then reload the page and if not, alert the user that the change has not happened.
The alert is just to see what is being returned by the .post function.
The auto_refresh works fine as i need it to check every 5 seconds, when the if() condition is set to '==' the page alert shows and if it is set to '!=' the page continually reloads.
jQuery.post is getting the db field data but it doesn't seem to be able to compare the 2 values correctly.
any help would be greatly appreciated, thanks
var auto_refresh = setInterval(function(){
$.post("/index.php/listen", function(data) {
if($('#slide').html() != data)
{
window.location.reload()
}
else
{
alert('its the same'+ data);
}
});
}, 5000);
EDITED
Rather than trying to parse raw data, why not pass HTML from the $.post() like:
<p>4</p>
Then the jQuery inserts the the replaces the p tag with the new version from the $.post()
because the html is passed on there is no white space and the comparison can be made correctly.
I don't think it is very safe to compare the new value with an html. Some browsers might add spaces or unwanted chars. I'd try to save the old value in an input of type hidden and use the .val() or, event better, in a variable. It depends of your scenario.
If $('#slide').html() == data
then that means that the conditional failed, it was not equal, so it showed the alert.
The problem is that the data variable might come back with a few extra white spaces. If I were you, I'd try to parse a small section of the data variable, and a small section of the html in slider and compare those values.
Like if slider has something within a p tag or an input value, compare it to the data to see if it has that same value returned in that p tag or input value, then replace all the whitespaces with an empty string just to be safe.
Btw, try not to use alerts since you can't really know for sure if there is an extra whitespace. Try to use something like "debugger" if using IE with visual studios, or console.log when using chrome or firefox.
You are comparing two html strings: one is serialized from the DOM, and another is from a server response.
There's no guarantee that the two strings will ever be the same! Think about it: the same rendered html can have many string differences. E.g. click and click are both the same HTML, but different strings.
You can take two different approaches here:
You can create some kind of canonicalization routine that guarantees that two html fragments you consider "the same" will have the same string form. Run both html fragments through this routine, then compare.
You can manage versions more explicitly.
Include some kind of version indicator:
You can use the ETAG header (which means you can take advantage of http caching mechanisms).
You can include some kind of version number in the html itself (maybe in a data-version attribute), and compare those.
You can keep the html string from your server separately and compare against that.

Extracting prices from html using jQuery

I'm parsing an html document and I need to extract all the prices in it (the format is $99.00). So what I want to do is extract all the elements that contain the substring "Price" (or "price") in its class or id attribute. But I tried using something like $("[class*='Price']") or $("[id*='Price']") and then concatenate the results on an array but the jquery selector part is not working properly, is not finding anything. Am I doing something wrong, or is there a better way to do this? Any suggestions for a better approach?
Thank you.
UPDATE:I'm actually using a jQuery port called phpQuery for php.
UPDATE2: I don't know the exact class or id of the elements since this is a generic script that I will run on different e-commerce sites, so that's why I'm using the *= wildcard to get all elements (mostly a, div, span, etc, I don't need input). I figured it out and this is what I have so far:
function getPrice($doc){
phpQuery::selectDocument($doc);
$prices = array();
foreach(pq("[class*='Price'], [class*='price'], [id*='Price'], [id*='price']") as $res){
$each = pq($res);
if(preg_match('/\$\d+(?:\.\d+)?/', $each->text(), $matches)){
echo '<br>'.$matches[0].'</br>';
$prices[] = $each->html();
}
}
}
This is printing the correct elements. Now I need to extract the font-size of those elements so I can sort the array by font-size.
Select by ID: $("#Price")
Select by Class: $(".Price")
Showing your HTML would help.
Give this ago
$(function(){
$('.price').each(function(){
//create your array here of $(this).val() or $(this).html()
});
});

Using PHP PCRE to fetch div content

I'm trying to fetch data from a div (based on his id), using PHP's PCRE. The goal is to fetch div's contents based on his id, and using recursivity / depth to get everything inside it. The main problem here is to get other divs inside the "main div", because regex would stop once it gets the next </div> it finds after the initial <div id="test">.
I've tryed so many different approaches to the subject, and none of it worked. The best solution, in my oppinion, is to use the R parameter (Recursion), but never got it to work properly.
Any Ideais?
Thanks in advance :D
You'd be much better off using some form of DOM parser - regex really isn't suited to this problem. If all you want is basic HTML dom parsing, something like simplehtmldom would be right up your alley. It's trivial to install (just include a single PHP file) and trivial to use (2-3 lines will do what you need).
include('simple-html-dom.php');
$dom = str_get_html($bunchofhtmlcode);
$testdiv = $dom->find('div#test',0); // 0 for the first occurrence
$testdiv_contents = $testdiv->innertext;

Categories