Click to See Complete Forum and Search --> : Getting rid of anything in a string thats not a number?


Conor
06-19-2004, 01:15 PM
I have a huge string, probably a few thousand characters long but all I need is the numbers. Is there anyway to get rid of all the letters.

simpson97
06-19-2004, 03:08 PM
This is one way:

<?php
$mystring = 'aswer1uuopo923grt6*&%$22';
$i = 0;
$parsed_number = "";
while($i < strlen($mystring))
{
$mychar = substr($mystring, $i++, 1);
if(ereg("[0-9]", $mychar)) $parsed_number .= $mychar;
}
echo $parsed_number;
?>

Bob

Conor
06-19-2004, 03:48 PM
that works thank you but is there anyway to return thnings in a readable manner. that just returns a very long string.

ex. This is the file Im grabbing and I want just the two bolded numbers listed like this

#1
#2, is that possible?

<a class="SubjectLink" title="60799458" href="/The_Vestibule/b5296/60799458/?11">blah blah</a>

</td>

<td nowrap class="BoardRowA">
<a href="http://users.ign.com/about/IX_Metroid-Prime_XI" class="AuthorLink">IX_Metroid-Prime_XI</a>
&nbsp;</td>

<td class="BoardRowB">

11

</td>

<td nowrap class="BoardRowA">
1:45pm
</td>
</tr>


<tr align="left" valign="top">
<td class="BoardRowB">
<img src="http://media.ign.com/boards/images/posticons/poll.gif" align="absmiddle" border="0" height="18" hspace="0" width="18" vspace="0">

<a class="SubjectLink" title="60800846" href="/The_Vestibule/b5296/60800846/?4">Why do people make polls</a>

simpson97
06-19-2004, 05:48 PM
Try this:

<?php

$str = '
<a class="SubjectLink" title="60799458" href="/The_Vestibule/b5296/60799458/?11">blah blah</a>
</td>
<td nowrap class="BoardRowA">
<a href="http://users.ign.com/about/IX_Metroid-Prime_XI" class="AuthorLink">IX_Metroid-Prime_XI</a>
</td>
<td class="BoardRowB">
11
</td>
<td nowrap class="BoardRowA">
1:45pm
</td>
</tr>
<tr align="left" valign="top">
<td class="BoardRowB">
<img src="http://media.ign.com/boards/images/posticons/poll.gif" align="absmiddle" border="0" height="18" hspace="0" width="18" vspace="0">
<a class="SubjectLink" title="60800846" href="/The_Vestibule/b5296/60800846/?4">Why do people make polls</a>
';

$str = strtolower($str);
$title_array = explode('<a class="subjectlink" title="', $str);
$num_array = array();
$ptr = 0;
for($i = 1; $i < count($title_array); $i++)
{
$tmp_array = explode('"', $title_array[$i]);
$num_array[$ptr++] = $tmp_array[0];
}
for($i = 0; $i < count($num_array); $i++)
{
echo '<b>' . $num_array[$i] . '<br>';
}

exit;

?>

Bob

pyro
06-19-2004, 07:11 PM
Originally posted by simpson97
This is one way:...Wouldn't this be easier?

<?PHP
$string = 'aswer1uuopo923grt6*&%$22';
$numbers = preg_replace('/\\D/', '', $string);
echo $numbers;
?>

Conor
06-19-2004, 11:39 PM
thank you simpsons97,that worked, thank you so much!

simpson97
06-20-2004, 01:14 AM
Very good.