Solved

us111 regular exp. again

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

 
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

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

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 …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 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 …

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