0

I have these php lines:

<?php
$start_text = '<username="';
$end_text = '" userid=';
$source = file_get_contents('http://mysites/users.xml');
$start_pos = strpos($source, $start_text) + strlen($start_text);
$end_pos = strpos($source, $end_text) - $start_pos;
$found_text = substr($source, $start_pos, $end_pos);
echo $found_text;
?>

I want to see just the names from entire file, but it shows me just the first name. I want to see all names.

I think it is something like: foreach ($found_text as $username).... but here I am stuck.



Update from OP post, below:

<?php
$xml = simplexml_load_file("users.xml");

  foreach ($xml->children() as $child)
  {
        foreach($child->attributes() as $a => $b)
        {
          echo $a,'="',$b,"\"</br>";

        }


               foreach ($child->children() as $child2)
    {

                foreach($child2->attributes() as $c => $d)
        {
          echo "<font color='red'>".$c,'="',$d,"\"</font></br>";
        }

    }
  }
?>

with this code, i receive all details about my users, but from all these details i want to see just 2 or 3

Now i see :

name="xxx"
type="default"
can_accept="true"
can_cancel="false"
image="avatars/trophy.png"
title="starter"
........etc

Another details from the same user "Red color(defined on script)"

reward_value="200"
reward_qty="1"
expiration_date="12/07/2012"
.....etc

what i want to see?

i.e first line from first column "name="xxx" & expiration_date="12/07/2012" from second column

1
  • it looks like you're trying to parse an XML file. If that's true, then (although it may be extra work), I suggest using a XML parser. That way, if you get dodgy input your whole system won't collapse. Commented Oct 30, 2011 at 11:20

2 Answers 2

1

You will need to repeat the loop, using the 3rd parameter, offset, of the strpos function. That way, you can look for a new name each time.

Something like this (untested)

<?php
$start_text = '<username="';
$end_text = '" userid=';
$source = file_get_contents('http://mysites/users.xml');
$offset = 0;
while (false !== ($start_pos = strpos($source, $start_text, $offset)))
{
    $start_pos += strlen($start_text);
    $end_pos = strpos($source, $end_text, $offset);
    $offset = $end_pos;
    $text_length = $end_pos - $start_pos;
    $found_text = substr($source, $start_pos, $text_length);
    echo $found_text;
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Looks good (+1), except $start_pos will effectively always be bool TRUE unless you wrap the assignment in parens - at the moment it will always be the result of a comparison operation, and since this is a while condition, it will always be TRUE or it won't be executed.
i tried with your code (i have modified your "endpos" to end_pos :) but i receive just first name from all names for many times(unlimited times) :) & also "Maximum execution time of 30 seconds exceeded in I:\xampp\htdocs\alt.php on line 12" on that line is $offset = $end_pos;
That's actually because end_pos wasn't really the end position, but the length of the text to copy. Therefor, $offset got the wrong value too (the length of the last text to be precise). I've changed this piece of code, to leave end_pos alone and introduce a new variable text_length. I also changed the condition in the while loop as advised by DaveRandom.
0

You should either use XMLReader or DOM or SimpleXML to read XML files. If you don't see the necessity, try the following regular expressions approach to retrieve all usernames:

<?php

$xml = '<xml><username="hello" userid="123" /> <something /> <username="foobar" userid="333" /></xml>';
if (preg_match_all('#<username="(?<name>[^"]+)"#', $xml, $matches, PREG_PATTERN_ORDER)) {
  var_dump($matches['name']);
} else {
  echo 'no <username="" found';
}

Comments

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.