?
Solved

explode with eregi_replace

Posted on 2002-03-03
11
Medium Priority
?
268 Views
Last Modified: 2012-06-21
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
Comment
Question by:Zuhair070699
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 4

Expert Comment

by:lokeshv
ID: 6838370
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
 

Author Comment

by:Zuhair070699
ID: 6838515
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
 
LVL 8

Expert Comment

by:us111
ID: 6838800
?
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

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

Any idea??
0
 
LVL 8

Expert Comment

by:us111
ID: 6841108
hmmm Zuhair still with with your regular expression question :))
well I'm trying but...
0
 

Author Comment

by:Zuhair070699
ID: 6845429
Any idea :(
0
 
LVL 11

Expert Comment

by:jimmy282
ID: 6855862
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
 

Author Comment

by:Zuhair070699
ID: 6857466
Thanks jimmy282 but how can I use it in my example?

Any idea?

Thanks
0
 
LVL 8

Accepted Solution

by:
us111 earned 800 total points
ID: 6857725
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
 

Author Comment

by:Zuhair070699
ID: 6860074
:) Very appreciated

Thanks us111
0
 

Author Comment

by:Zuhair070699
ID: 6870563
Hi us111

I notice that your code is not working with string like

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

Any idea,, :(
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question