Solved

us111 regular exp. again

Posted on 2002-03-17
8
167 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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 …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 dynamically set the form action using jQuery.

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