0
<?php 

$list = "<tr>
          <td>1.</td>
          <td>aaa</td>
          <td>234234</td>

          <td>1.</td>
          <td>bbb</td>
          <td>23423423</td>
        </tr>
        <tr>
          <td>2.</td>
          <td>ccc</td>

          <td>5644</td>
          <td>2.</td>
          <td>ddd</td>
          <td>4566456</td>
        </tr>
        <tr>
          <td>3.</td>

          <td>eee</td>
          <td>456456</td>
          <td>3.</td>
          <td>fff</td>
          <td>456456456</td>
        </tr>
"; ?>

CODEPAD: http://codepad.org/JaxTfBF6

How can i get all values from second and the fifth TD in each TR from variable $list? i would like receive array. I can use PHP or jQuery.

$all = array('aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff');
1
  • If you already have the data server-side, why would you want to send it to client-side ,parse it with jQuery, and then back to server-side? Commented Feb 2, 2012 at 14:08

6 Answers 6

2
var text = "<tr><td>1.</td><td>aaa</td><td>234234</td><td>1.</td><td>bbb</td><td>23423423</td></tr><tr><td>2.</td><td>ccc</td><td>5644</td><td>2.</td><td>ddd</td><td>4566456</td></tr><tr><td>3.</td><td>eee</td><td>456456</td><td>3.</td><td>fff</td><td>456456456</td></tr>";

var output = $(text).find("td:nth-child(2), td:nth-child(5)").map(function() {
    return $(this).text();
});

console.log(output);
Sign up to request clarification or add additional context in comments.

Comments

1

This would be pretty easy with jQuery.

You could load DOM objects from the string:

var values = [];
var domObj = $( input_string );

$( 'tr:nth-child(5)', domObj ).each( function() {

   values.push( this.val() );
});

Comments

1

With PHP you can use DOM or simpleXML libraries to parse HTML or regular expressions.

Comments

1

You could use jQuery:

var list = $('td:nth-child(2), td:nth-child(5)').map(function(){
   return $(this).text();
}).get()

It basically retrieves the fifth element of every td and then applied a function to each element, which replace the element itself with its text content.

Comments

1

It seems as if you already have the data on your server, so there is no reason to send it to the client, use jQuery to parse it, and then send it back to PHP.

Given your input array $list in PHP, you can do the following.

First match all <td> elements

if (preg_match_all('/<td>(.*)<\/td>/U', $list, $res))
    $res = end($res);

Then filter out all the wrong rows.

$count = 0;
$res = array_filter($res, function($item) use (&$count) {
    return (++$count%3==2);
});

Finally remove the old keys in the array and return it.

$res = array_values($res);
print_r($res);

The above should return

Array (
    [0] => aaa       [1] => bbb       [2] => ccc
    [3] => ddd       [4] => eee       [5] => fff
)

Comments

0

Similar to the other answers, but complete working code...

var all = [];

$("tr").each(function() {
    $(this).find("td:nth-child(2)").each(function() {
        all.push($(this).val());
    });
    $(this).find("td:nth-child(5)").each(function() {
        all.push($(this).val());
    });
})

2 Comments

No reason for downvotes? I'd like an explanation so I know what's wrong with the code.
@PaulAttuck Thanks Paul, but I just wanted to know what people though was wrong with it. I tested it and it works so if it's bad code then I'd rather know why so I can learn from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.