Hi,

i am trying to extract number out from a text which i grab from the database. EG: ACK9734N, I would like to extract just the number 9724 from the text, how can i do so?

i understand about using preg_match() but when i tried to do that, the number came out in an array one at a time which is not what i want.

Can someone be able to help me?? Thanks

Cheers

Recommended Answers

All 7 Replies

- Thanks for the website.

- Been searching all over google on preg_match_all and just cant a good example. Yours indeed a good one.

Once again, thanks for your help.

Hi,

i am trying to extract number out from a text which i grab from the database. EG: ACK9734N, I would like to extract just the number 9724 from the text, how can i do so?

i understand about using preg_match() but when i tried to do that, the number came out in an array one at a time which is not what i want.

Can someone be able to help me?? Thanks

Cheers

To add on, it works with the help of "preg_replace" to extract the number out from the text/string. Is there other way or command to use if i am to extract a certain range of number that i want.

ie: "FBD #6 (1532) "

I want to extract either 6 or 1532 from the above. Can this be done by using preg_replace or other method. I've tried to use preg_replace and the result came out to be 61532. As i need to use this information as a wild card to do a search on another database so the number is critical.

thanks

Member Avatar for diafol

Sorry, preg gives me a nosebleed. I assume you need to search for brackets and numbers or the hash (#) symbol, depending on what you need. If all the strings have the same format, it *should* be straightforward. If not, yuk, I feel a headache coming on!

Sorry, preg gives me a nosebleed. I assume you need to search for brackets and numbers or the hash (#) symbol, depending on what you need. If all the strings have the same format, it *should* be straightforward. If not, yuk, I feel a headache coming on!

I need to do the search base on the numbers inside the bracket or the single number come after the #.
can this be done?

=)

Member Avatar for diafol

Most definitely. Don't ask me how though - well with preg functions anyway.

I could do it with strpos() and substr():

$hashstart = strpos($term,"#") + 2;
$hashnum = substr($term,$hashstart,1);
$bracketstart = strpos($term,"(") + 1;
$bracketend = strpos($term,")") ;
$bracketnum = substr($term,$bracketstart,$bracketend-$bracketstart);

So $hashnum will give you the single number and $bracketnum should give you the digits within the brackets. This is most definitely NOT the best way to do things, but you could use it as a stop-gap until somebody better gives you a proper preg solution.

*note there's no validation or error checking involved - this should be worked in.

Thanks mate,

Got it working. will implement to my project.
Will post again if i encounter any other problems.

Cheers and have a lovely weekend.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.