Span ID value in PHP

How to get span text in in php variable?

In the above span, i am getting id from .js file. eg., 45 and i want that id in php variable. eg., $cid=’45’; I have already tried below code, but i am unable to get id.

 '; ?> loadHTML($str); $items = $DOM->getElementsByTagName('span'); $span_list = array(); for($i = 0; $i < $items->length; $i++) < $item = $items->item($i); $span_list[$item->getAttribute('class')] = $item->nodeValue; > extract($span_list); echo $cid; ?> 

3 Answers 3

try this one add span text in php code to get the span value in variable

Please try the below code and check at your end you can get class value in the array.

 45'; ?> loadHTML($str); $items = $DOM->getElementsByTagName('span'); $span_list = ''; for($i = 0; $i < $items->length; $i++) < $item = $items->item($i); if($item->getAttribute('class') == 'cid')< $span_list = $item->nodeValue; > > echo $span_list; ?> 

Code for multiple span tags and get single value from that span list array.

 45 48'; ?> loadHTML($str); $items = $DOM->getElementsByTagName('span'); $span_list = array(); for($i = 0; $i < $items->length; $i++) < $item = $items->item($i); if($item->getAttribute('class') == 'cid')< $span_list[] = $item->nodeValue; > > //get the each value for multiple span tag foreach ($span_list as $key => $value) < echo $value; echo '
'; > ?>

Источник

How to Get the inner text of a span in PHP

Ok, i have creted my own drop down box. I have a Div, and in the div i have a span. I need to grab that inner span text. How do i do that? The span’s text does change. I am 15, so give me a break! I need some help! This is were the values are picket.

  

PHP runs on the server and produces HTML. If you want to do things dynamically in the browser, that’s what JavaScript is for.

Читайте также:  Edittext android example java

1 Answer 1

Ok, you’re going to need more than just PHP to get this done; you’ll need JavaScript as well.

Let’s start with your HTML. I’m assuming your rendered output looks like the following and I won’t question why you’re doing it this way.

 
Option One Option Two Option Three

So there’s my guess at your HTML.

To post a value back to PHP, you’re also going to need a way to capture the selected value in an input field that can be posted with a form. A hidden input will probably be the best option for you.

So that’s our markup done. Next you’ll need to grab the selected option from your div and stick it into the hidden field. I’m assuming you’ve coded something to render your div to look and behave exactly like a dropdown (ok, I’ll bite. Why ARE you doing it this way?).

This is the JavaScript code using jQuery (we only use jQuery on StackOverflow. Just kidding; that’s not true. Well, maybe it’s a little bit true)

Now, as long as you’ve ensured that the hidden field is contained within a form that posts back to your target PHP page, you’ll have the value of the span tag available to you.

I’ve made quite a few assumptions about how you’ve gone about setting up your page here. Correct me on any parts that don’t tie into reality.

Источник

How to get span tag content using preg_match function? [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

1 Answer 1

The Better Solution

A regex is the wrong tool here. HTML is not a regular language, and cannot be accurately parsed using regular expressions. Use a DOM parser instead. Not only is it much easier, it’s more accurate and reliable, and won’t break when the format of the markup changes in the future.

Читайте также:  Maximum likelihood method python

This is how you would get the contents inside a tag using PHP’s built-in DOMDocument class:

$dom = new DOMDocument; $dom->loadHTML($yourHTMLString); $result = $dom->getElementsByTagName('span')->item(0)->nodeValue; 

If there are multiple tags, and you want to get the node values from all of them, you could simply use a foreach loop, like so:

$dom->loadHTML($html); foreach ($dom->getElementsByTagName('span') as $tag) < echo $tag->nodeValue . '
'; >

And finally, to extract just the number from the node value, you have several options:

// Split on space, and get first part echo explode(' ', $result, 2)[0]; // Replace everything that is not a digit or comma echo preg_replace('/[^\d,]/', '', $result); // Get everything before the first space echo strstr($result, ' ', 1); // Remove everything after the first space echo strtok($result, ' '); 

All these statements will output 414,817 . There’s a whole host of string functions available for you to use, and you can choose one solution that suits your requirements.

The Regex-based solution

If you absolutely must use preg_match() , then you can use the following:

if (preg_match('#]*>([\d,]+).*?#', $result, $matches))
[^<>]* means «match any number of characters except angled brackets«, ensuring that we don’t accidentally break out of the tag we’re in.

.*? (note the ? ) means «match any number of characters, but only as few as possible«. This avoids matching from the first to the last tag in the markup (if there are multiple s).

I make absolutely no guarantees that the regex will always work, but it should be enough for those who want to finish up a one-off job. In such cases, it’s probably better to go with a regex that works on sane things than weep about things not being universally perfect 🙂

Источник

A solution to Get span id value in PHP

A solution to Get span id value in PHP

In this article, you going to see how to get span tag of html value assign to the PHP variable. We going to see in a step by step.

Let’s start with the solution for getting span value in PHP in a detail.

You going to see this with the help of example.

Читайте также:  Disable path length limit при установке python

To store the span text into PHP variable, we going to achieve this using below example.

Below is the example from which you can understand completely.

       Printed PHP variable total amount value : '.$output = $_POST['total'].''; > ?> 

+

= (Store in Span id value) Rs.

OUTPUT ON FORM SUBMIT

With help of Student fees calculation you can understand completely.

In the above example you can see two input box one for Math fees and another one is for English fees.

I am calculating both subject fees using Jquery.

I taking both input value using Jquery and then passing into function where calculation is done.

After the completion of the calculation it is assign to html input tag element inside form.

When the total value of the both fees get inserted inside input tag name total of form.

An important step to assign to PHP :

You can see the output above.

Now step is to how the value is get populated using Jquery.

On click of submit button the form value get posted.

This posted value is get assign to PHP variable.

In this form is get posted on click of submit button and value is get assigned to PHP value.

Finally we done with the assign of span data to php variable.

This posted total variable you can also use to insert this data to into database.

To insert the posted value into database you can use the SQL statement query.

Below is syntax for SQL statement query.

INSERT INTO table_name VALUES (value1, value2, value3, value4. );

Below is descriptions regarding parameter and its value.

Parameter Description
value1 It is the first value to be inserted.
value2 It is the second value to be inserted.
value3 It is the third value to be inserted.
value4 It is the fourth value to be inserted.

Insert statement query

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.

Источник

Оцените статью