Solved

us111 regular exp. again

Posted on 2002-03-17
8
170 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.
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 …

726 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