Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 11-02-2011, 12:30 AM Reg-EX?
Novice Talker

Posts: 10
Trades: 0
Hello Everybody! I have a regex problem that I can't figure out. How can I get the first and last names of all people that has these letter tay in their name(s) using php-RegEX.

Here is a sample of my PhoneBook setup:
Dori Hanson:733-555-9501:25 E. Pine, Chicago, IL 60660
Evon Taylor:312-555-0706:29 W. Troy, Chicago, IL 60626
Lucy Bintay:312-555-2295:18 E. Lunt, Chicago, IL 60639

This is only one of many sequence I tried. This will print the entire line and not just the full name only which is what I am trying to do:
Code:
<?php
$fhandle=fopen("MyPhoneBook.txt", "r");
    while( ! feof($fhandle))  {
        $myBook= fgets($fhandle);
 
        if (preg_match("/(tay)/i", $myBook))
        {
            echo "$myBook<br>";
    }
}
fclose($fhandle);
?>
Thanks in advance

Last edited by mgraphic; 11-07-2011 at 10:34 AM.. Reason: edit sample phone numbers
max21b is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-02-2011, 01:25 AM Re: Reg-EX?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You can accomplish this without a regex:
PHP Code:
$lines file('MyPhoneBook.txt'); /*file reads an entire file into an array of lines*/

$matches = array();

foreach(
$lines as $line)
{
     
$name substr$line0strpos($line':') ); /*get the portion of the line before the colon*/
     
if(stripos($name'tay') !== false)
          
$matches[] = $name;
}

print_r($matches); 
The above should output:
Code:
Array
(
    [0] => Evon Taylor
    [1] => Lucy Bintay
)
Personally I don't think there is any particular advantage to using a regex over the solution above.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 11-02-2011, 02:28 AM Re: Reg-EX?
Novice Talker

Posts: 10
Trades: 0
Thanks NullPointer, but it's all about filling my RegEX file with examples mainly for future reference and for building my first PhoneBook. I been reading everything I could find and it's not as easy as they say. I'm sure you been there, done that and know what I mean. Once you start getting to know something you don't just drop it. Would you know how to do this using php - regular expression? If not, Thanks a ton anyway
max21b is offline
Reply With Quote
View Public Profile
 
Old 11-02-2011, 09:16 AM Re: Reg-EX?
Novice Talker

Posts: 10
Trades: 0
The code just replace actually only allow Reg-EX to do half of the job. The whole string was still beings used but un-seen. I need to know how to do it completely using reg-EX the right way. See my first thread for full explanation.

chrishirst just made me realize that both of our expressions does the same thing and that is not the desired results. I thought I had it, I'm going to try again tonight.

Code:
<?php
$fhandle=fopen("MyPhoneBook.txt", "r");
    while( ! feof($fhandle))  {
        $myBook= fgets($fhandle);

		if (preg_match("/(tay)/i", $myBook))
		{
			echo "$myBook<br>";
	}
}
fclose($fhandle);
?>


Code:
<?php
$fhandle=fopen("MyPhoneBook.txt", "r");
    while( ! feof($fhandle))  {
        $myBook= fgets($fhandle);

		if (preg_match("/.*tay.*/", $myBook))
		{
			echo "$myBook<br>";
	}
}
fclose($fhandle);
?>

Last edited by max21b; 11-02-2011 at 12:30 PM.. Reason: bad-cut and paste: Now it works! .. i hope
max21b is offline
Reply With Quote
View Public Profile
 
Old 11-02-2011, 11:31 AM Re: Reg-EX?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
.*tay.* would be the pattern to match the entire line where a search of 'tay' was found.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 11-02-2011 at 11:36 AM..
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Reg-EX?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21526 seconds with 12 queries