Solved

explode with eregi_replace

Posted on 2002-03-03
11
257 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Permalink issue in Wordpress archive 1 32
php date - compare 4 40
PHP warning 4 28
Intermittent Error on Page Loading 4 6
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.

932 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

12 Experts available now in Live!

Get 1:1 Help Now