Solved

How do i detect the % character in a string

Posted on 2009-06-28
6
234 Views
Last Modified: 2012-05-07
... and avoid detecting all characters as if the "%" was being used as a wifld card.
I wish to run a test like if (string contains % character) then flag = "percent"
0
Comment
Question by:marjog
6 Comments
 
LVL 14

Expert Comment

by:profya
ID: 24730830
Try this:
if (preg_match("/^[0-9\.]{1,}(\s)*%/", "200%"))
	echo "percentage";
else
	echo "not perccentage"; 

Open in new window

0
 
LVL 5

Expert Comment

by:landship
ID: 24730922
profya's solution would flag ".%" or "9   %" as a percentage, also it is anchored to the beginning of the string so a percentage in the middle of the string would not be found. A fix to the regular expression to find floating numbers properly would be:
if (preg_match("/\d*(\.?\d+)+\s?%/", "200%")) // or .2% or 2.3 %
        echo "percentage";
else
        echo "not perccentage";

Open in new window

0
 
LVL 5

Expert Comment

by:landship
ID: 24730960
Actually if someone was using the % character as a wild card in a string of numbers, perhaps for a date (200% to figure out any year 2000-2009) it would get miss-flagged as a percentage.

I could think of other ways to flag the % symbol if you give me a better idea of how your code will be used.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 14

Expert Comment

by:profya
ID: 24730977
lanship:
Thanks for the fix., there was an error matching .%. I understood that data are textual, that's why it is possible to find something like
200  %
The author seems to me wants to parse any possible percentage, the output can be fixed in another stage. It is better than excluding it as I think.
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 250 total points
ID: 24731372
>> I wish to run a test like if (string contains % character) then flag = "percent"

if(strpos($string,'%')!==false) $flag = "percent";

This test does not try to detect if it is used in conjuction with a number or not, it just detedts if the is any % character present.
0
 

Author Closing Comment

by:marjog
ID: 31597655
cxr's interpretation of my needs, "detects if the is any % character present", was coorect.
Thanks to all who responded.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Insert values are dynamic 11 41
tutorial for ebay api 3 35
how to use 2 SELECT statements with prepared statements 19 30
PHP Syntax Error 4 27
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question