Solved

explode with eregi_replace

Posted on 2002-03-03
11
261 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
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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 200 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 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