By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,883 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,883 IT Pros & Developers. It's quick & easy.

RegEx with preg_replace

P: n/a
Hello,

I have a string like this:

var1: value1...valueI var2: value1...valueJ ... varN:
value1...valueK

this is an example:

breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
cake

I'm trying to use preg_replace to change "lunch:" values to a code
number, in order to get in the example:

breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake

keeping the rest of variables and values unchanged.

Take in account, that I'm only sure that I have the "lunch:" word
delimitation (and its values), I don't know if exists "breakfast:" or
"dinner:"; but I'm sure that if the variable exists, then the values
exists too.

Thanks,

Sebastian.
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Sebastian Araya wrote:
this is an example:

breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
cake

I'm trying to use preg_replace to change "lunch:" values to a code
number, in order to get in the example:

breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake

keeping the rest of variables and values unchanged.


Not sure if preg_* is the way to go ... but, what the hell :-)

<?php
$foods['00'] = 'coffee';
$foods['01'] = 'wine';
$foods['02'] = 'cake';
$foods['03'] = 'apple';
$foods['04'] = 'eggs';
$foods['05'] = 'sandwich';
$foods['06'] = 'chicken';
$line = 'breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine cake';

preg_match('/^(breakfast: .* )?lunch: (.*)( dinner:.*)?$/U', $line, $meal);
$liunch = explode(' ', $meal[2]);

echo $meal[1], 'lunch: ';
foreach ($lunch as $foodname) echo array_search($foodname, $foods);
echo $meal[3], "\n";
?>
Output of this script is:
breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
Use preg_replace_callback(). In the callback function, parse the list of
lunch items and look up the corresponding code for each.
In the regexp you need to use preg's lookbehind feature, to look for the
string "lunch:" but keeping it out of the match. The following snippet
should yield the right result:

$s = "breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
cake";

$food_ids = array( 'coffee' => '00', 'wine' => '01', 'cake' => '02',
'apple' => '03', 'eggs' => '04', 'sandwich' => '05', 'chicken' => '06' );

function food_to_code($matches) {
global $food_ids;
$foods = preg_split('/\\s+/', $matches[0]);
$codes = "";
foreach($foods as $food) {
$code = $food_ids[$food];
if($code) {
$codes .= $code;
}
}
return "$codes ";
}

echo preg_replace_callback('/(?<=lunch:\\s)(?:\\w+(?:\\s|$))+/',
'food_to_code', $s);

Uzytkownik "Sebastian Araya" <ar*****@numisys.com.ar> napisal w wiadomosci
news:67*************************@posting.google.co m...
Hello,

I have a string like this:

var1: value1...valueI var2: value1...valueJ ... varN:
value1...valueK

this is an example:

breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
cake

I'm trying to use preg_replace to change "lunch:" values to a code
number, in order to get in the example:

breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake

keeping the rest of variables and values unchanged.

Take in account, that I'm only sure that I have the "lunch:" word
delimitation (and its values), I don't know if exists "breakfast:" or
"dinner:"; but I'm sure that if the variable exists, then the values
exists too.

Thanks,

Sebastian.

Jul 17 '05 #3

P: n/a
A more efficient and compact code to achieve the same with minor
modifications to ignore multiple space characters in string:

[SNIP]

$s = "breakfast: coffee eggs lunch: sandwich apple dinner:
chicken wine
cake";

$food_ids = array( 'coffee' => '00', 'wine' => '01', 'cake' =>
'02',
'apple' => '03', 'eggs' => '04', 'sandwich' => '05', 'chicken' =>
'06' );

function food_to_code($matches)
{
return preg_replace('/\w+/e','$GLOBALS[\'food_ids\'][\'$0\']',
trim($matches[0]));
}

echo preg_replace_callback('/(?<=lunch:\s)(?:[\w\s]+)(?=\s\w+:\s|$)/',
'food_to_code',$s);

[/SNIP]

A little more compact code (i guess more *efficient* also &
complicated too):
[/SNIP]

$s = "breakfast: coffee eggs lunch: sandwich apple dinner:
chicken wine
cake";

$food_ids = array( 'coffee' => '00', 'wine' => '01', 'cake' =>
'02',
'apple' => '03', 'eggs' => '04', 'sandwich' => '05', 'chicken'
=> '06' );

$pattern = '/(?<=lunch:\s)(?:[\w\s]+)(?=\s\w+:\s|$)/e';
$replacement = 'preg_replace(\'/\\w+/e\',\'$food_ids[\\\'\$0\\\']\'
,trim(\'$0\'))';

echo preg_replace($pattern, $replacement,$s);

[/SNIP]

--

Wish you all Happy new year!!

Cheers!
Rahul
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<cc********************@comcast.com>...
Use preg_replace_callback(). In the callback function, parse the list of
lunch items and look up the corresponding code for each.
In the regexp you need to use preg's lookbehind feature, to look for the
string "lunch:" but keeping it out of the match. The following snippet
should yield the right result:

$s = "breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
cake";

$food_ids = array( 'coffee' => '00', 'wine' => '01', 'cake' => '02',
'apple' => '03', 'eggs' => '04', 'sandwich' => '05', 'chicken' => '06' );

function food_to_code($matches) {
global $food_ids;
$foods = preg_split('/\\s+/', $matches[0]);
$codes = "";
foreach($foods as $food) {
$code = $food_ids[$food];
if($code) {
$codes .= $code;
}
}
return "$codes ";
}

echo preg_replace_callback('/(?<=lunch:\\s)(?:\\w+(?:\\s|$))+/',
'food_to_code', $s);

Uzytkownik "Sebastian Araya" <ar*****@numisys.com.ar> napisal w wiadomosci
news:67*************************@posting.google.co m...
Hello,

I have a string like this:

var1: value1...valueI var2: value1...valueJ ... varN:
value1...valueK

this is an example:

breakfast: coffee eggs lunch: sandwich apple dinner: chicken wine
cake

I'm trying to use preg_replace to change "lunch:" values to a code
number, in order to get in the example:

breakfast: coffee eggs lunch: 0503 dinner: chicken wine cake

keeping the rest of variables and values unchanged.

Take in account, that I'm only sure that I have the "lunch:" word
delimitation (and its values), I don't know if exists "breakfast:" or
"dinner:"; but I'm sure that if the variable exists, then the values
exists too.

Thanks,

Sebastian.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.