• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

explode with eregi_replace

Hi,

I want to manuplate every string between [test] and [/test] by explode it and return new string.

For example I have the following string:

$string="many lines
[test]word1|word2|word3[/test]
";

Then I want to replace :

[test]word1|word2|word3[/test]

By:

1: word1
2: word2
3: word3


I use:

$string=eregi_replace("\[test\]([^\\[]*)\[/test\]",words('\1'),$string);

where «words» is a function:

function words($mywords){
   $line=explode("|",$mywords);

   $lines="1: $line[0] <BR>  2: $line[1] <BR>  3: $line[2] <BR>";


return $lines;
}



What is wronge in my code ??

Thanks
0
Zuhair070699
Asked:
Zuhair070699
1 Solution
 
lokeshvCommented:
i m not convinced the idea by which u r passing the string to words fuction

check it again..busy morn so time to test ur code.

anyway user str_replace instead of ereg_replace, much fater

for more check this

http://www.php.net/manual/en/function.str-replace.php

Lk
0
 
Zuhair070699Author Commented:
Thanks,

But what I am looking for is how to explode the match result of eregi_replace!

The code is:

<?

$string="many lines ....<BR>
[test]word1|word2|word3[/test]

<BR>
many lines ....<BR>
<BR>

[test]word1|word2|word3[/test]
<BR>
many lines ....
";

$string=eregi_replace("\[test\]([^\\[]*)\[/test\]",words('\1'),$string);


function words($mywords){
  $line=explode("|",$mywords);

  $lines="1: $line[0] <BR>  2: $line[1] <BR>  3: $line[2] <BR>";


return $lines;
}

echo $string;

?>



The output shoud be like:
==========================

many lines ....
1: word1
2: word2
3: word3

many lines ....

1: word1
2: word2
3: word3

many lines ....



==================

THANKS
0
 
us111Commented:
?
function words($mywords)
{  $line=explode("|",$mywords);
   for($i=0;$i<count($line);$i++)
   { $newline.= ($i+1).":".$line[$i]."\n";
   }
   return $newline;
}

$string="many lines [test]word1|word2|word3[/test] ";

$string=ereg("\[test\]([^\\[]*)\[/test\]",$string, $res);
print words($res[1]);
?>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Zuhair070699Author Commented:
Thanks us111, but what I am looking for is to replace all occurance of [test]...[/test] not only the first one.

Any idea??
0
 
us111Commented:
hmmm Zuhair still with with your regular expression question :))
well I'm trying but...
0
 
Zuhair070699Author Commented:
Any idea :(
0
 
jimmy282Commented:
I think eregi_replace is not an option here.

You should use preg_match_all.
http://www.php.net/manual/en/function.preg-match-all.php

Just an idea.

Jimmy
0
 
Zuhair070699Author Commented:
Thanks jimmy282 but how can I use it in my example?

Any idea?

Thanks
0
 
us111Commented:
I've got it worked :

<?
function words($mywords)
{
   $line=explode("|",$mywords);

   $newline="<br>";
   for($i=0;$i<count($line);$i++)
   { $newline.= ($i+1).":".$line[$i]."<br>";
   }

   return $newline;
}

$string="many lines ....<BR>
                     [test]word1|word2|word3[/test]

                     <BR>
                     many lines ....<BR>
                     <BR>

                     [test]word4|word5[/test]
                     <BR>
                     many lines ....
                     ";

$r = preg_replace("/(.*)(\[test\])(.*)(\[\/test\])(.*)/e", "words('\\3')", $string);
print $r."\n";
?>
0
 
Zuhair070699Author Commented:
:) Very appreciated

Thanks us111
0
 
Zuhair070699Author Commented:
Hi us111

I notice that your code is not working with string like

$string="[test]
         word1|
         word2|
         word3
         [/test]";

Any idea,, :(
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now