1

I'm trying to convert textarea text into a string I can put into the postcodes.io checker API

Example of textarea:

TW7 8PX
TW14 8NU
W3 7JW

This then needs to convert to:

"TW7 8PX", "TW14 8NU", "W3 7JW"

Currently I am using a manual string:

$query = '"TW7 8PX", "TW14 8NU", "W3 7JW"';
$postcode = new Postcode();
$lookup = $postcode->bulkLookup(array($query));

I imagine it's achievable with preg_replace but I can't seem to find the combination I need.

gview
  • 14,876
  • 3
  • 46
  • 51
Joseph Gregory
  • 579
  • 1
  • 7
  • 24
  • Your `$query` line will cause a syntax error. Please update your desired output to something valid so that the answerers can provide an accurate response. – mickmackusa Jun 28 '17 at 21:06
  • @mickmackusa it didn't cause any issues as this is taken right off the test page I was using. However I have reached an answer. Thank you anyway :) – Joseph Gregory Jun 28 '17 at 21:09
  • Your question has actually resulted in wasting the time of several volunteers because they are trying to pack extra double-quotes into the string. You have solved your own question as you understand it, not as the question states. See the error: http://sandbox.onlinephpfunctions.com/code/d96d39c20b80af33d08d392ec3d428ee81d40da7 Please update your question. – mickmackusa Jun 28 '17 at 21:27
  • @mickmackusa apologies that was never my intention – Joseph Gregory Jun 28 '17 at 21:42
  • @mickmackusa I'll grant you that the $query line is invalid and misleading, but the initial information was clear enough that people were able to answer the question. Furthermore, I don't feel that this question was about explode, nor answered by explode, even if explode is part of the solution. Give the information provided, my solution illustrates that the proper formatting needed for a solution is inherent in the use of json_encode. – gview Jun 28 '17 at 21:54

6 Answers6

3

You can achieve this by using only single line of code:

$result = '"' . implode('", "', explode("\n", $inputString)) . '"';
unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
3

Regular expressions are for matching a pattern, if your pattern is just a single character (newline in this case), they are overkill.

$query = '"'.str_replace("\n", '", "', $_POST['textarea']).'"';

Would do what you want.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
3

Seems like everyone here missed the obvious -- this is input for a REST api that wants json.

Here's the solution:

<?php

$input = <<< EOT
TW7 8PX
TW14 8NU
W3 7JW
EOT;

$output['postcodes'] = explode("\n", $input);
echo json_encode($output);

Tested

gview
  • 14,876
  • 3
  • 46
  • 51
2

This is a possible solution:

<?php
$input = <<< EOT
TW7 8PX
TW14 8NU
W3 7JW
EOT;
foreach (explode("\n", $input) as $line) {
  $output[] = '"' . $line . '"';
}
var_dump(implode(', ', $output));

An alternative would be:

<?php
$input = <<< EOT
TW7 8PX
TW14 8NU
W3 7JW
EOT;
$output = '"' . implode('", "',explode("\n", $input)) . '"';
var_dump($output);

The obvious output is:

string(31) ""TW7 8PX", "TW14 8NU", "W3 7JW""
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Too much code for simple task. – unalignedmemoryaccess Jun 28 '17 at 18:38
  • @tilz0R Indeed it appears bloated, certainly any programmer can implement something more crunchy. However consider the way the quote chars have to be added... – arkascha Jun 28 '17 at 18:41
  • The *alternative* approach is the one. Or using `str_replace` is even much better and easier. – unalignedmemoryaccess Jun 28 '17 at 18:42
  • @tilz0R I oppose the wide spread habit to claim that there is only one, the "right" solution to a task. The first alternative is more flexible, for example it allows to simply add a call to `trim()` if desired... – arkascha Jun 28 '17 at 18:43
  • It is not that hard, still single line and very efficient: `$output = '"' . implode('", "', array_map('trim', explode("\n", $input))) . '"';` – unalignedmemoryaccess Jun 28 '17 at 18:44
  • @tilz0R much harder to read. Keeping code in a single line is _not_ a virtue! – arkascha Jun 28 '17 at 18:45
  • @tilz0R Actually, the first approach would technically be better if for example there were no values posted. The second option would result in `""` as output where the first would be an empty string. My answer has the same issue. – Jonathan Kuhn Jun 28 '17 at 18:45
1

Give this a try:

$textAreaText = //whatever the field the value resides
$convertedText = '"' . implode('", "',explode("\n", $textAreaText)) . '"';

Output "TW7 8PX", "TW14 8NU", "W3 7JW"

Tested on PhpFiddle

Rushikumar
  • 1,774
  • 5
  • 18
  • 28
1

It turns out it was very simple I was just looking at it the wrong way:

$query = explode("\n", str_replace("\r", "", $_POST["postcodes"])); //Use Explode
$postcode = new Postcode();
$lookup = $postcode->bulkLookup($query); // Remove array()

Thank you for all your help

Joseph Gregory
  • 579
  • 1
  • 7
  • 24
  • 2
    This is actually poor form, in you adding an answer to your own question, when many people provided you essentially this same answer, as well as all the techniques you utilized. Also your question omitted that apparently you used an api for the postal code service, but didn't provide information on what that api requires as input. Also to @mickmackusa's point, your question has an invalid line of php code in it, when you do the assignment to $query. You should remove or fix that line from your question. – gview Jun 28 '17 at 22:03
  • From the link in your post I looked at the github for the various php libraries. There are 4 listed: https://github.com/ideal-postcodes/postcodes.io/ Not one that I looked at requires the quotation format, as most are working with an array, so it would be good to know which one you are actually using here. In the future people working with postcodes.io may find this question and it might have value if the question is complete. – gview Jun 28 '17 at 22:38