Solved

# ereg function?????

Posted on 2002-04-09
202 Views
hai all,

How to check my string
WSEP_23B
using ereg function
I use this below code cannot achieve my target.
ereg("^([A-Z]{4})."_".([1-9]{1,2}[A_Z]\$",trim(\$value[2]))

can anyone help me
bye
0
• 6
• 2
• 2

LVL 4

Expert Comment

you just wann to check the specific string (WSEP_23B)

or the pattern..
??

Lk
0

Author Comment

I actually want to check the pattern.

but I don't mind knowing the coding to check for the specifc string also.

thanks

0

Author Comment

I actually want to check the pattern.

but I don't mind knowing the coding to check for the specifc string also.

thanks

0

Author Comment

I actually want to check the pattern.

but I don't mind knowing the coding to check for the specifc string also.

thanks

0

LVL 4

Expert Comment

for specific string .search use string funtions.

like

str_replace()
substr_count();

for more check this ...

http://www.php.net/manual/en/ref.strings.php
0

Author Comment

I wanted to use ereg function only.
0

LVL 6

Accepted Solution

andreif earned 25 total points
Hi,

you have some mistakes in your expression

"^([A-Z]{4})."_".([1-9]{1,2}[A_Z]\$"

"^([A-Z]{4})_([1-9]{1,2}[A-Z])\$"

problem 1: expression ."_". in the centre is confusing, because it splits string into two "^([A-Z]{4})." and ".([1-9]{1,2}[A_Z]\$" and _ between those string must cause an error

problem 2: A_Z should read as A-Z - just a mistype
problem 3: closing brace ) was missed

That's all!
0

Author Comment

hai andreif,
I actually check thru if condition like

if (strlen(trim(\$value[2])) == 8 && ereg("^([A-Z]{4})_([1-9]{1,2}[A-Z])\$",trim(\$value[2])));

echo "<td>".trim(\$value[2])."</td>";
else
//display \$value in red color in browser.

as u told I change the coding but still I can see the value in red color ie it takes the else condition only.

can u advice where I went wrong.

Thank u
0

LVL 6

Expert Comment

Hi,

it seems correct, at least it works at me.

Can you give an examples of \$value[2] values?

Note:
1. expression is CAse SenSitive now :)
2. you don't have to check length, because regular expression has require rather strict string format, it allows strings like

ABCD_1E or ABCD_12E, so if you need only strings with 2 digits (length=8) you should replace [1-9]{1,2} with [1-9]{2} and don't worry about length
0

Author Comment

hai andreif,

I got it.

I just did a simple mistake.
I added the coding as u gave now works well
Thank u
0

## Featured Post

### Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 dynamically set the form action using jQuery.