Click to See Complete Forum and Search --> : Regexp / Explode help


callumd
07-03-2008, 04:06 PM
Hi, I have a text file that's list of Enlgish words, with their Arabic counterparts. Here's a sample:

Abrams = أبرامز
Abramson = أبرامسون
Abrasion = الجرح
Abrasions = الجروح
Abrasive = المزيل
Abrasively = بخشونة
Abrasiveness = الحكّ
Abrasives = المزيلات
Abreact = حرّر
Abreacted = حرّر
Abreacting = تخليص من العقد
Abreaction = تحرّر من عقدة نفسيّة
Abreactions = تحرّر من عقدة نفسيّة
Abreacts = يحرّر
Abreast = جنبًا إلى جنب
Abridge = اختصر
Abridged = اختصر
Abridgement = الاختصار
Abridgements = الاختصارات
Abridger = الملخّص
Abridges = يختصر
Abridging = الاختصار
Abridgment = الاختصار
Abroad = بالخارج
Abrogate = ألغ
Abrogated = ألغى
Abrogates = يلغي

The goal: To explode all of the Arabic words in to an array.

I am guessing I'll need to use preg_replace() to take out the english words and the extraneous spaces, and then use explode() to get all of the Arabic words in to an array.

HELP!

Phill Pafford
07-03-2008, 04:22 PM
try something like this


// create Arabic Array
$arabic_arr = Array();

// Read file into string
$str = file_get_contents('/path/to/file.txt');

// explode on equal sign = into array
$arr = explode("=",$str)

foreach($arr as $key => $value)
{
// filter out Engilsh
if(!preg_match("/^[a-zA-Z]+$/", $value))
{
// trim
$value = trim($value);

// Push to new array
array_push($arabic_arr, $value);
}
}

// to see the value of the Arabic Array
print_r($arabic_arr);

NogDog
07-03-2008, 04:32 PM
This might work:

$text = file_get_contents('path/to/file');
preg_match_all('/=\s*([^\r\n]+)(?:[\r\n]|$)/', $text, $matches);
print_r($matches[1]); // $matches[1] contains first regexp sub-pattern matches

callumd
07-03-2008, 04:54 PM
Thanks guys,

I'll try them out.

Xlayer
07-04-2008, 06:43 PM
This might work:

$text = file_get_contents('path/to/file');
preg_match_all('/=\s*([^\r\n]+)(?:[\r\n]|$)/', $text, $matches);
print_r($matches[1]); // $matches[1] contains first regexp sub-pattern matches


short, very nice ! Thanks. ;)