Solved

Replace words in textboxes with conditions

Posted on 2006-07-12
4
240 Views
Last Modified: 2008-03-04
I have an example here that I am unable to figure out. I get stuck when I go to replace a word that is not in the original array. I posted an html version to see if someone could write the php code from scratch for this. I would like to see how it differs from mine and where I went wrong. This contains the instructions of what to do.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test</title>
<style type="text/css">
<!--
.style1 {font-family: Arial, Helvetica, sans-serif; font-size: 10px; }
-->
</style>
</head>

<body>

<form id="form1" name="form1" method="post" action="">
  <label></label>
  <table width="815" height="225" border="1">
    <tr>
      <td colspan="2" rowspan="5"><p class="style3"><span class="style1">Textbox 1
          </span>
        <textarea name="field1" cols="30" rows="10" id="field1">red bull energy
juice drink
red bull hot cakes
fruit baskets
fruit juice
peanut butter and red bull
red bull energy drink
fine wine
berry jelly
red fruit baskets</textarea>
          </p>
        <p>&nbsp;</p>      </td>
      <td width="96" class="style1"><span class="style3">Phrase to replace in each line </span></td>
      <td width="144"><input name="replacethis" type="text" id="replacethis" value="fruit baskets" /></td>
      <td width="405" rowspan="5"><p class="style1">Results 1 = textbox1 </p>
        <p class="style1">Results 2 = If a line within textbox 1 contains the phrase to replace, then replace phrase with Alt 1,  otherwise if the phrase to replace is not in a line within textbox 1, ignore the line. (Do not return the line) </p>
        <p class="style1">Results 3 =  If a line within textbox 1 contains the phrase to replace, then replace phrase with Alt 2,  otherwise if the phrase to replace is not in a line within textbox 1, ignore the line. (Do not return the line) </p>
        <p class="style1">Results 4 =  If a line within textbox 1 contains the phrase to replace, then replace phrase with Alt 3,  otherwise if the phrase to replace is not in a line within textbox 1, ignore the line. (Do not return the line) </p>
        <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    </tr>
    <tr>
      <td class="style1"><span class="style3">Alternative 1 </span></td>
      <td><input name="r2" type="text" id="r2" value="gift baskets" /></td>
    </tr>
    <tr>
      <td class="style1"><span class="style3">Alternative 2 </span></td>
      <td><input name="r3" type="text" id="r3" value="gifts" /></td>
    </tr>
    <tr>
      <td class="style1"><span class="style3">Alternative 3 </span></td>
      <td><input name="r4" type="text" id="r4" value="baskets" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
  <label><br />
  </label>
  <table width="592" border="1">
    <tr>
      <td class="style1">Results 1 </td>
      <td class="style1">Results 2 </td>
      <td class="style1">Results 3 </td>
      <td class="style1">Results 4 </td>
    </tr>
    <tr>
      <td class="style1"><textarea name="t5" cols="30" rows="10" id="t5">red bull energy
juice drink
red bull hot cakes
fruit baskets
fruit juice
peanut butter and red bull
red bull energy drink
fine wine
berry jelly
red fruit baskets</textarea></td>
      <td class="style1"><textarea name="t6" cols="30" rows="10" id="t6">gift baskets
red gift baskets</textarea></td>
      <td class="style1"><textarea name="t7" cols="30" rows="10" id="t7">gifts
red gifts</textarea></td>
      <td class="style1"><textarea name="t8" cols="30" rows="10" id="t8">baskets
red baskets</textarea></td>
    </tr>
    <tr>
      <td class="style1"><p>Here is what should appear </p>
      </td>
      <td class="style1">Here is what should appear:</td>
      <td class="style1">Here is what should appear:</td>
      <td class="style1">Here is what should appear:</td>
    </tr>
  </table>
  <label><br />
  </label>
</form>
</body>
</html>
0
Comment
Question by:ray-solomon
[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
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 17096698
Hi,

I would go with something like this:

if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['field1']) && !empty($_POST['r2']) && !empty($_POST['r3']) && !empty($_POST['r4']) && !empty($_POST['replacethis'])) {
  $source = $_POST['field1'];
  $needle = $_POST['replacethis'];
  $alt1     = $_POST['r2'];
  $alt2     = $_POST['r3'];
  $alt3     = $_POST['r4'];

  $results = array(1 => '', 2 => '', 3 => '');
 
  $lines = preg_split("/[\n\r]+/", $source);
  foreach($lines as $line) {
     if(strstr($line, $needle)) {
        $results[1] .= str_replace($needle, $alt1, $line)."\n";
        $results[2] .= str_replace($needle, $alt2, $line)."\n";
        $results[3] .= str_replace($needle, $alt3, $line)."\n";
     }
  }

 echo '<textarea name="results1" rows="5" cols="40">'.$results[1].'</textarea>';
 echo '<textarea name="results2" rows="5" cols="40">>'.$results[2].'</textarea>';
 echo '<textarea name="results3" rows="5" cols="40">>'.$results[3].'</textarea>';
}

?>

-r-
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 17097419

if (isset($_POST["replacethis"])){
      $textarea="";
      $texts=$_POST["field1"];
      $replacestr=$_POST["replacethis"];
      preg_match_all('/(.*?'.$replacestr.'.*?)/', $texts, $result, PREG_PATTERN_ORDER);
      for ($i = 0; $i < count($result[0]); $i++) {
            $textarea.=$result[0][$i]."\r\n";
            
            }
      $alternates=array($_POST["r2"],$_POST["r3"],$_POST["r4"]);
      foreach($alternates as $val){
            $result = preg_replace('/'.$replacestr.'/', $val, $textarea);
            echo nl2br($result);
            }
      }
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17097562
It's very late I will test this in the morning.
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17102604
Thanks Roonaan, This was the part I was not understanding and now I get it. Much appriciated.

$results = array(1 => '', 2 => '', 3 => '');
 
  $lines = preg_split("/[\n\r]+/", $source);
  foreach($lines as $line) {
     if(strstr($line, $needle)) {
        $results[1] .= str_replace($needle, $alt1, $line)."\n";
        $results[2] .= str_replace($needle, $alt2, $line)."\n";
        $results[3] .= str_replace($needle, $alt3, $line)."\n";
     }
  }
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
while read ID; do 4 58
deprecated mysql extensions 1 30
Delete image(s) associated with record(s) 16 29
Set a time limit on Wordpress Cookie function. 3 16
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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…

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