How do you extract fred from "<company>fred</company>"?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: How do you extract fred from "<company>fred</company>"?

Post by mecha_godzilla »

You can still use htmlspecialchars() if you need to convert the data, but whether you should do this or not depends on how you're going to use it in your script:

Code: Select all

$romancartxml = $_POST['romancartxml'];

$count = preg_match("/[\<]first-name[\>](.*)[\<]\/first-name[\>]/s", "$romancartxml", $firstname);
$count = preg_match("/[\<]postcode[\>](.*)[\<]\/postcode[\>]/s", "$romancartxml", $postcode);
$firstname = htmlspecialchars($firstname[0]);
$postcode = htmlspecialchars($postcode[0]);

echo "$firstname $postcode";
HTH,

M_G
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract fred from "<company>fred</company>"?

Post by simonmlewis »

Thanks.
Oddly, without htmlspecialchars, I was able to output the variable as the word inside those 'tags/headers'.
With two extracted, I'll be able to update our database much more efficiently. Great job.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract fred from "<company>fred</company>"?

Post by simonmlewis »

Code: Select all

<?php 
$romancartxml = $_POST['romancartxml'];

$count = preg_match("/[\<]primarystockremaining[\>](.*)[\<]\/primarystockremaining[\>]/s", "$romancartxml", $primarystockremaining);
$count = preg_match("/[\<]item-code[\>](.*)[\<]\/item-code[\>]/s", "$romancartxml", $itemcode);
$primarystockremaining = $primarystockremaining[0];
$itemcode = $itemcode[0];
echo "Processing $itemcode, $primarystockremaining";

include "dbconn.php";
$result = mysql_query ("SELECT id, romancode, players FROM events WHERE romancode = '$itemcode'") or die(mysql_error());
$num_result = mysql_num_rows($result);
echo "$num_result for $itemcode";
while ($row = mysql_fetch_object($result))
  {
  } mysql_free_result($result);
 mysql_close($sqlconn);
 echo "<br/>$itemcode";
?>
Apologies for the extra code at the bottom, but this is odd. $itemcode echos perfectly. And if I force $itemcode = "070313-19";, then this query pulls up a result of 1. Which is correct.

But if I just query it like this, it pulls up nothing.
I know there is a way to kind of "see" the query, to know if it is putting the itemcode into the query correctly, but don't know how. It's really bizarre. I thought this would be the easy bit!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract fred from "<company>fred</company>"?

Post by simonmlewis »

Update: If I post the itemcode via a normal non-xml type way, it works.
But if I do it the way I am here, it won't. Here is the current code that still doesn't work.

Code: Select all

<?php 
$romancartxml = $_POST['romancartxml'];

$count = preg_match("/[\<]primarystockremaining[\>](.*)[\<]\/primarystockremaining[\>]/s", "$romancartxml", $primarystockremaining);
$count = preg_match("/[\<]item-code[\>](.*)[\<]\/item-code[\>]/s", "$romancartxml", $itemcode);
$primarystockremaining = $primarystockremaining[0];
$itemcode = $itemcode[0];
echo "<br/>$itemcode";
include "dbconn.php";
$result = mysql_query ("SELECT id, romancode, players FROM events WHERE romancode = '$itemcode'") or die(mysql_error());
while ($row = mysql_fetch_object($result))
  {
  echo "$row->players";
  } mysql_free_result($result);
 mysql_close($sqlconn);
?>
echo "<br/>$itemcode";...... this does echo the itemcode.
But $row->players produces nothing, yet it should. What the heck is happening?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract fred from "<company>fred</company>"?

Post by simonmlewis »

I am getting somewhere with this now, but still stumped:

Code: Select all

<?php 
$romancartxml = $_POST['romancartxml'];

$count = preg_match("/[\<]primarystockremaining[\>](.*)[\<]\/primarystockremaining[\>]/s", "$romancartxml", $primarystockremaining);
$count2 = preg_match("/[\<]item-code[\>](.*)[\<]\/item-code[\>]/s", "$romancartxml", $itemcode);
$primarystockremaining = $primarystockremaining[0];
$itemcode = $itemcode[0];

echo "<br/>$itemcode, $primarystockremaining
<form method='post' action='#'>
 <input type='text' name='test' value='$itemcode'>
 <input type='submit'>
 </form>";
include "dbconn.php";
$result = mysql_query ("SELECT id, romancode, players FROM events WHERE romancode = '$itemcode'") or die(mysql_error());

while ($row = mysql_fetch_object($result))
  {
  echo "$row->players";
  } mysql_free_result($result);
 mysql_close($sqlconn);
?>
This echos the attached screenshot.
So it's not taking the content out, and storing it in a variable. How in HTML do I keep that contents as the item-code, without the outter parts??
Attachments
This is what is produced.
This is what is produced.
ss.jpg (13.75 KiB) Viewed 936 times
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you extract fred from "<company>fred</company>"?

Post by Christopher »

preg_match is returning what it matched. That includes the <item-code> and </item-code>. You need to use substr() to remove the tags.
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you extract fred from "<company>fred</company>"?

Post by requinix »

Or use $primarystockremaining[1] and $itemcode[1].
Post Reply