Solved

us111 regular exp. again

Posted on 2002-03-17
8
169 Views
Last Modified: 2006-11-17
Hi us111,

Regarding to your answer in:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=php&qid=20272809

I notice that your code is not working with string like

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

Any idea,, :(
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
  • 4
  • 4
8 Comments
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6876021
If you can put up your regular expression here I can tell you whats wrong.

JD
0
 

Author Comment

by:Zuhair070699
ID: 6876029
<?
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
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6876037
This works.

<?
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\])(.*)/emx", "words('\\3')", $string);
print $r."\n";
?>

jd
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:Zuhair070699
ID: 6876053
:( The question is if I have more than one line between [test] and [/test]

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


Try this code for example:
=============================
<?
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\])(.*)/emx", "words('\\3')", $string);
print $r."\n";
?>

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

Thanks
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6876060
Oh,

So you have new line inbetween your search string,
in that case you need to add modifier PCRE_DOTALL , (small s)

see this works,

<?
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]

                 ";




$r = preg_replace("/(.*)(\[test\])(.*)(\[\/test\])(.*)/emxs", "words('\\3')", $string);
print $r."\n";
?>


But here if you put up text, where multiple [text] [/text] are there, last one would be printed.

JD
0
 

Author Comment

by:Zuhair070699
ID: 6876099
:( sorry this will not solve my problem, is there a way to replace multiple [text] [/text] ??
0
 
LVL 5

Accepted Solution

by:
dkjariwala earned 100 total points
ID: 6876143
This works !! :)
I had to try lot of modifiers but it was great learning experience !! Thx !!

<?
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\])((?s).*)(\[\/test\])(.*)/eU", "words('\\3')", $string);
print $r . "<br>";

?>

jd
0
 

Author Comment

by:Zuhair070699
ID: 6876160
:) Thank you very much ....
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

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 …
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 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