You are invited to Log in or Register a free Frihost Account!

Formulate Regular Expression

AOP Web Development
Hello guys, what i want is to formulate a regular expression that can fetch the words only.. I hope you can help me by this

I have a sample data

$data=23278164 sexy african singles 2006-05-28 14:40:15

all i need is the to fetch the words the output would be like this

$fetchdata= sexy african singles

i hope you can help me about this thanks a lot Wink


  $data="23278164 sexy african singles 2006-05-28 14:40:15 4hello5 hello8";
  preg_match_all('/\b[a-zA-Z]+\b/', $data, $matches);
  $fetchdata = implode(" ", $matches[0]);
  echo $fetchdata;

Only those words are matched which:
* contain only alphabetic letters (a-z, A-Z)
* are seperated by non-alphanumerics from non-words.
AOP Web Development
Hello thank you for the the effort but when i try the code there something that i have noticed.. about it

the every single word has been but to the array and aside from that i there something that i would like to exclude.. sorry for that i didn't put all the exact file... actually supposedly the the data is is coming from the textfile width supposed to be having a five fields in every row to but i only need is the first 3 fields to be input into the database.

this is the sample data

23219931 open call for print magazines 2006-05-08 12:52:41 95

but i only need the first 3 fields to be fetch.

since i already get the field 1 and field3 by means of by using the regexp i used

$pattern = "/^.{1,8}|([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})|([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/";

by that i can now fetch the field1and field3 but i want to fetch the only problem is how to fetch the field 2

by your regexp you give they try to fetch the website address/field5 which is not included to be fetch.

$pattern = "/\b[a-zA-Z]+\b/";

i just like also what is the purpose on the '\b' you put on the first and last... thanks.. in advance
:wink: :wink:
Can you post the lines of your program which does the matching? So I can see how you use the result-arrays. It makes it easier to change it without breaking the part of your program which depend on the result-array.

Or, if it can easily be changed, you can use this pattern:






  $data="23219931 open call for print magazines 2006-05-08 12:52:41 95";
  $pattern = '/\d+\s+(.*?)\s+([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})\s*([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/';
  preg_match($pattern, $data, $matches);
  echo "Text: '$matches[1]'<BR>";
  echo "Date: $matches[2]-$matches[3]-$matches[4]<BR>";
  echo "Time: $matches[5]:$matches[6]:$matches[7]<BR>";
  // output:
  // Text: 'open call for print magazines'
  // Date: 2006-05-08
  // Time: 12:52:41


Here I changed your pattern a bit (by removing the or (|) and adding the part which matches the 2nd field in front of it) to make the pattern match the entire line at once and you can process all the lines by using preg_match once on each line. If this doesn't do what you want or breaks the code you already have, please post your code.

The '\b' (which isn't in this pattern) means a 'word boundery': i.e. it matches any place between a word-character ([a-zA-Z0-9]) and a non-word character (every other character or the beginning or end of the string).


Oh, and how do you post a pattern without getting smilies in it? When I post this without code-tags:
it has smilies, but in your post it doesnt Think Surprised
AOP Web Development
Actually the data i have made a sample is a content of a text file and it supposed to be imported into the database. but all i need is the the first 3 fields

supposedly this is the the content of the txtfile.

23219931 106 in park auditions 2006-05-08 19:54:26 8
23219931 106 in park auditions 2006-05-08 19:55:23 15
23219931 106 in park auditions 2006-05-08 19:55:23 19

actually the content of it is having a different spaces.

So this is what i did to the code is to get the content of the txt file by rows. But this the first solution i think about it.



$matches = array();
$pattern = "/^.{1,8}|([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})|([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/";

foreach($fileline as $key=>$value){





So that is my first solution that i think in order to got this one but i just realize that i instead of using pregmatching by using regexp, i found out another solution which also get the exact result and here is it


      $lines = file("txtfile.txt");
         foreach($lines as $linenum=> $value){
            $sString = str_replace($arrReplace,"[del]",$value);
            $arr = explode("[del]",$sString);
            echo $arr[0]."+".$arr[1]."+".$arr[2]."<br>";


and now it totally get the first 3 fields of the content on the txtfile, in easy way.

but anyway thank you for the effort for solving and helping to solve it. ehehheehe

regarding the by displaying the pattern not displaying the smilies... i just disable the smilies for the post. So that you can completely see the pattern .

Related topics
Echoing Titles
Perl server referencing
PHP - Find and Delete
PHP Regex
mod_rewrite code
HTML: Automatically add ALT tags
[JavaScript]: Checking for undefined values
My Favorite Portable Applications
[JS] Regular expressions global match get first group
Help with User Form Validation
regular expression
Complicated regular expression (for preg_replace)
Help with regular expression
Python Expression Evaluator Version 2
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.