Solved

us111 regular exp. again

Posted on 2002-03-17
8
165 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
 

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

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

18 Experts available now in Live!

Get 1:1 Help Now