Question:

Help with regular expression in php

by  |  earlier

0 LIKES UnLike

I'm having a problem where I cant seem to get the contents around an expression to be matched. This expression works just fine:

$expression = '/<input type=\"text\" class=\"pagetext\" NAME=\"qty\" size=3 value=\"626\" MAXLENGTH=\"3\">/i';

echo preg_replace($expression,"ya",$result);

This will replace the input box within the source with the word 'ya'. What I want it to do is to replace the entire page with the word 'ya'. so I tried:

$expression = '/^.*<input type=\"text\" class=\"pagetext\" NAME=\"qty\" size=3 value=\"626\" MAXLENGTH=\"3\">.*$/i';

echo preg_replace($expression,"ya",$result);

And the expression couldnt be found. I have dumbed the expression down...its main function will eventually be to parse out the numeric number 626(which is an unknown number).

 Tags:

   Report

1 ANSWERS


  1. That probably happens because $result contains a newline character. The dot metacharacter in preg_replace doesn&#039;t match newlines unless the &quot;s&quot;-modifier is set. This should work:

    $expression = &#039;/^.*&lt;input type=\&quot;text\&quot; class=\&quot;pagetext\&quot; NAME=\&quot;qty\&quot; size=3 value=\&quot;626\&quot; MAXLENGTH=\&quot;3\&quot;&gt;.*$/is&#039;;

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.