Solved

explode with eregi_replace

Posted on 2002-03-03
11
250 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now