Friday, September 21, 2012

Convert Text Area to Array PHP

Processing multiple records through Text Area, We may need to convert that text area values into array from every line of text in that TextArea. here is my solution to "Convert TextArea to Array"

<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Text Area to Array</title>

// Check Form Submission
 // Capture cities text into variable
 $text = ucwords($_POST['cities']);
 // Replace entry new line with Comma(,)
 $cities = preg_replace("~\s*[\r\n]+~", ', ', $text);
 // Explode by Comma(,) and trim if any white spaces with array_map
 $cities = array_map('trim',explode(",",$cities));
 // final output as array
 echo "<pre>";
 echo "</pre>";


<form action="" method="post">
  <h2>Convert Text Area to Array</h2>
  <p>Enter each city every line<br />
    <textarea name="cities" cols="50" rows="10" id="keywords"></textarea>
    <br />
    <br />
    <input name="submit" type="submit" value="Submit Cities" id="submit" />

Download This Script     Live Demo    

No comments:

Post a Comment