PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Sep 19, 2018 1:09 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Aug 09, 2012 11:50 am 
Offline
Forum Newbie

Joined: Tue Mar 20, 2012 9:14 am
Posts: 2
Hello,

I am trying to set a variable to a number from a string. The number can be 1 to 4 characters long. Right now I have it returning the index for the start of the number, I need it to return the number itself.

In the example below I want to captrue the '581'. This is in AS3 by the way.

Any suggestions?

Syntax: [ Download ] [ Hide ]
width="581"
 


Top
 Profile  
 
PostPosted: Thu Aug 09, 2012 1:15 pm 
Offline
Forum Contributor
User avatar

Joined: Thu May 11, 2006 8:58 pm
Posts: 305
Location: Utah, USA
If the string is html, it would be best to parse it with DOMDocument. Otherwise you can use preg_match.


Top
 Profile  
 
PostPosted: Thu Aug 09, 2012 5:23 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
A regex option:

Syntax: [ Download ] [ Hide ]
$string = 'width="581"';
$regex = '~(?i)width="(\d{1,4})~';
$hit = preg_match($regex,$string,$match);
if($hit) echo $match[1].'<br />';
 


Now go read up on regex to understand why that works.
:D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group