473,788 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_match digits?

What is the most efficient way of extracting the first two digits in a
string?

The following is wrong for me, because it only gives me the first
instance of two digits together:

$string = ujdk3ca94abc
preg_match("/\d{2}/",$string,$resu lt);
echo "$result[0]";
//prints 94. However, the result I am looking for is 39

Here's another example:
$string = 'abc 8a bc abc934';
//desired result = 89

Thank you.

Jul 17 '05 #1
14 7183
In article <40************ ***@nospamun8no spam.com>, Westcoast Sheri wrote:
What is the most efficient way of extracting the first two digits in a
string?

The following is wrong for me, because it only gives me the first
instance of two digits together:


$found = '';

$index = 0;

while (strlen($found) < $needed && $index < strlen($string) ) {
if (is_numeric($st ring{$index})) {
$found .= $string{$index} ;
}
++$index;
}
--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Jul 17 '05 #2
Tim Van Wassenhove wrote:
In article <40************ ***@nospamun8no spam.com>, Westcoast Sheri wrote:
What is the most efficient way of extracting the first two digits in a
string?

The following is wrong for me, because it only gives me the first
instance of two digits together:


$found = '';

$index = 0;

while (strlen($found) < $needed && $index < strlen($string) ) {
if (is_numeric($st ring{$index})) {
$found .= $string{$index} ;
}
++$index;
}

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>


Tim, that's a joke, right?


Jul 17 '05 #3
In article <40************ ***@nospamun8no spam.com>, Westcoast Sheri wrote:
Tim Van Wassenhove wrote:
In article <40************ ***@nospamun8no spam.com>, Westcoast Sheri wrote:
> What is the most efficient way of extracting the first two digits in a
> string?
>
> The following is wrong for me, because it only gives me the first
> instance of two digits together:
$found = '';

$index = 0;

while (strlen($found) < $needed && $index < strlen($string) ) {
if (is_numeric($st ring{$index})) {
$found .= $string{$index} ;
}
++$index;
}

Tim, that's a joke, right?


1-) It works.
2-) I presume it's more efficient than using regular expressions.

Up to you proove me wrong :D
--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Jul 17 '05 #4
Westcoast Sheri wrote:
What is the most efficient way of extracting the first two digits in a
string?


<?php
function digits($string, $length, $pad) {
$temp = preg_replace('=[^0123456789]=', '', $string);
for ($i = 0; $i < $length; ++$i) $temp .= $pad;
return substr($temp, 0, $length);
}

$string = 'ujdk3ca94abc';
echo digits($string, 2, '0');
$string = 'abc 8a bc abc934';
echo digits($string, 2, '0');
?>

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #5
Pedro Graca wrote:
Westcoast Sheri wrote:
What is the most efficient way of extracting the first two digits in a
string?


<?php
function digits($string, $length, $pad) {
$temp = preg_replace('=[^0123456789]=', '', $string);
for ($i = 0; $i < $length; ++$i) $temp .= $pad;
return substr($temp, 0, $length);
}

$string = 'ujdk3ca94abc';
echo digits($string, 2, '0');
$string = 'abc 8a bc abc934';
echo digits($string, 2, '0');
?>

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :


thanks! That looks like what I was looking for. Although, I thought there
was a "code" you could put after the preg_match part just to return the
first 2 digits.... I guess there's not? Also, here's another question: what
does the "6" mean in the following line of code? I know the "5" means to
find 5 digits in a row....but what's the "6" mean?
preg_match("/[0-9]{5,6}/",$string,$blah )
Jul 17 '05 #6
Tim Van Wassenhove wrote:
In article <40************ ***@nospamun8no spam.com>, Westcoast Sheri wrote:
Tim Van Wassenhove wrote:
In article <40************ ***@nospamun8no spam.com>, Westcoast Sheri wrote:
> What is the most efficient way of extracting the first two digits in a
> string?
>
> The following is wrong for me, because it only gives me the first
> instance of two digits together:

$found = '';

$index = 0;

while (strlen($found) < $needed && $index < strlen($string) ) {
if (is_numeric($st ring{$index})) {
$found .= $string{$index} ;
}
++$index;
}

Tim, that's a joke, right?


1-) It works.
2-) I presume it's more efficient than using regular expressions.

Up to you proove me wrong :D

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>


Answer:

<?php
$string = 'a 20adiidkk4kidid 39399399dkkdkkk dk';
$dig = substr(preg_rep lace("/\D/",'',$string),0 ,2);
echo $dig;
?>

Takes .000027 to run.
Jul 17 '05 #7
Pedro Graca wrote:
Westcoast Sheri wrote:
What is the most efficient way of extracting the first two digits in a
string?


<?php
function digits($string, $length, $pad) {
$temp = preg_replace('=[^0123456789]=', '', $string);
for ($i = 0; $i < $length; ++$i) $temp .= $pad;
return substr($temp, 0, $length);
}

$string = 'ujdk3ca94abc';
echo digits($string, 2, '0');
$string = 'abc 8a bc abc934';
echo digits($string, 2, '0');
?>


That's a very flexible function and you're my god. :-) Nevertheless, if
you just need what the original poster asked for, you could use a simple
regular expression:

function digits2($string ) {
$return = '';
if (preg_match('~( \d)[^\d]*(\d)~', $string, $matches)) {
$return = $matches[1] .$matches[2];
}
return $return;
}

Regards,
Matthias
Jul 17 '05 #8
Westcoast Sheri schrieb:
thanks! That looks like what I was looking for. Although, I thought there
was a "code" you could put after the preg_match part just to return the
first 2 digits.... I guess there's not? Also, here's another question: what
does the "6" mean in the following line of code? I know the "5" means to
find 5 digits in a row....but what's the "6" mean?
preg_match("/[0-9]{5,6}/",$string,$blah )


Minimum:5, Maximum:6.

Regards,
Matthias
Jul 17 '05 #9
In article <40************ ***@nospamun8no spam.com>, Westcoast Sheri wrote:
Tim Van Wassenhove wrote:
1-) It works.
2-) I presume it's more efficient than using regular expressions.
Up to you proove me wrong :D
Answer:

<?php
$string = 'a 20adiidkk4kidid 39399399dkkdkkk dk';
$dig = substr(preg_rep lace("/\D/",'',$string),0 ,2);
echo $dig;
?>

Takes .000027 to run.


Feel free to run the following script a few times ;)
test1 always had a smaller execution time than test2 when i tried....
<?php

function test1($string, $needed) {
$found = '';
$index = 0;

while (strlen($found) < $needed && $index < strlen($string) ) {
if (is_numeric($st ring{$index})) {
$found .= $string{$index} ;
}
++$index;
}
return $found;
}

function test2($string) {
$dig = substr(preg_rep lace("/\D/",'',$string),0 ,2);
return $dig;
}
$strings = array(
'12sqdmfksdjflm qsdfjlm',
'sdfsqdf3mkjmkl j4',
'a56qsdfmqsdfkj ',
'sdf7dqfdsf8dqf df',
'a 20adiidkk4kidid 39399399dkkdkkk dk'
);

$start = microtime();
foreach($string s as $string) {
test1($string,2 );
}
$end = microtime();
echo '<br>total: ' . ($end - start);

$start = microtime();
foreach($string s as $string) {
test2($string);
}
$end = microtime();
echo '<br>total: ' . ($end - start);

?>

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4246
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100 gifs). then it processes them based off of which image type it selected, either the st- 's or the sb- 's.
2
4697
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out = 3456789 preg_match ("|product=(\d{5,10})&amp;|i", $html, $out);
6
1742
by: lkrubner | last post by:
I apologize for my igorance of Regex. I can't get this function to work. It gives the following error message. function checkForFloatingPoint($input=false) { $pattern = "?(*\.+|+)"; $match = preg_match (pattern, subject); echo "The match is: $match "; return $match;
5
7539
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match, $res_description); if (!$valid_srch) { ...
6
10252
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i", $_FILES) I understand the basic preg_match but am confused as to how the string pattern part is working i.e. "/\." . $allowed_file_types . "$/i"
2
1950
by: David | last post by:
Help how can I force the variable to start with a character. I dont want it to start with a number???? function alphanumeric($alphanumeric_field) { if(!preg_match("/+$/",$alphanumeric_field)) return TRUE;
2
4255
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
13
5373
by: chadsspameateremail | last post by:
I might have found a problem with how preg_match works though I'm not sure. Lets say you have a regular expression that you want to match a string of numbers. You might write the code like this: preg_match( '/^+$/', $TestString ); OK everything seems fine. However, did you know if you pass the following to preg_match: "12345\n" it will return that a match occurred?!? Even though the newline is not a valid character in our regular...
8
4001
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ='~^.~' ); // make sure there is always a match
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.