I manually echo some data in a .csv file. For each row, I use the following code :
...(some data)."\n";
Now, I want to use a form where the user can input the end of row. For instance, if he wants his rows to be ended by "\n", he types : \n in the form.
My problem is how to handle this value in the PHP echo code ?
I've tried .$var, ."$var", I also tried to escape the double quotes, but I can't manage to reach my goal.
Thanks
You could instead make them type a character that they wouldn't use as content, for example | might work.
Then all you'd have to do is replace | by \n inside the string, you can do it this way:
$content = str_replace ('|','\n', $content);
Related
I have stored some html data into database through summernote plugin
in database it look like this
<p><span id="job_summary" class="summary"><ul><li>
<b class="jobtitle"><font size="+1">Analyst/Junior Analyst- Outbound calling process</font></b>
here is how i show it
echo text_cut(strip_tags(html_entity_decode($ro)),300);
now i want to show this data in plain text on my page, i tried using strip_tags but it makes the looks messy, here is how it looks after strip tags
knowledgeMust be reliable in terms of attendance and timingExhibit
it joined the words, so now i want all the html tags to be converted into how can i achieve this
Try This,
$spaceString = str_replace('<', ' <', $ro);
echo strip_tags(html_entity_decode($spaceString));
Here's something you can try. If you replace the tags with a space and then replace multiple spaces with one space, it should give you the desired results.
First, use something like this to replace the tags:
preg_replace('~<.*?>~i', ' ', $string);
Here is what it will give you
Next, you can look for multiple spaces in a row and consolidate them:
preg_replace('~ +~', ' ', $string);
That will give you this:
Analyst/Junior Analyst- Outbound calling process
Here is a demo of it all together
You won't really be able to see it, but there is a line above it with a blank space and a blank space before the string as well. So, depending on what you are wanting the result to look like, you can use a \s+ instead of [SPACE]+
Here is another demo showing how to do it that way
I am trying to split a string that i pass from a HTML textarea.
Enter the list of Ids: <textarea rows="4" cols="50" name="ids">
I get it in a php script as
$idlist=$_GET["ids"];
When I try to split it, I couldnt get it with explode. Tried out with multiple delimitiers that could possibly split this and then tried using preg_split. I gave this as my regex.
$ids=preg_split("/\s/",$idlist);
Now that i split, I get empty array elements in the $ids. And I couldnt match them like
$ids[$i]==""|" "|"\s"
1) How will the text in text area be passed? I am giving one Id per line in the text area. It doesnt not carry new line character but a single space.
2) Will preg_split split out even the delimitiers that one uses to split the string?
I know I am making a blunder but couldnt figure out where. Someone any thoughts?
If each ID is on a new line, you need to explode by the new line character \n...
$ids=preg_split("/\r|\n)*/", $idlist);
EDIT: Updated to support windows encoding due to it being sent as a GET not a POST..
Explanation:
Windows send new lines as \r\n so if it is a GET the you need to provide support for this...
Here you go - just using explode to break them down into an array based on the "\n" new line character. I know you mentioned this does not work, but tested it and it works fine for me:
<?php
if (isset($_GET['ids'])) {
$arrayOfIds = explode("\n", $_GET['ids']);
}
?>
<form method="get" action="">
<textarea name="ids"></textarea>
<button type="submit">Submit</button>
</form>
As you mentioned above, when typing ID's into the textarea field I just put them each on a new line.
Figured it out. There was a single space as well as a New line character in the string. Exploded it with \n and then trimmed it with trim() that solved the problem.
With a textarea you have to assume the user is stupid when you ask them to provide data. If you say "Provide an id on each line" assume they will not read it and use commas or assume they press ENTER 50 times at the end, or assume they pasted it in from MS Word. No offense to the user, but just assume they are a Jelly fish on a keyboard and at some point it manages to click "submit".
Taking that into account, (and I'm assuming here that your ids are numbers) it means that anything that isn't a number can be ignored. This makes worrying about new lines, be that \n\r or whatever, completely irrelevant as long as there is some kind of character between each id then that's all we need.
I'm going to change $_GET["ids"] to $_POST["ids"] because I'd be posting that sort of data.
// This will create a list of ids with NO care about what delimeter
// the user used regardless of what you'd requested.
// Replace all non-numeric characters with #
$ids = preg_replace("/[^0-9]/is","#",$_GET["POST"]);
// Remove recursive # and trim
$ids = preg_replace("/#{1,}/is","#",trim($ids,"#"));
// Create array ensuring ids isn't empty
// This also removes unique id entries
$ids = empty($ids)?array():array_unique(explode("#",$ids));
// Show array
echo "<pre>";
print_r($ids);
echo "</pre>";
I want to parse the value from a textarea based on linebreaks and have it converted into one string replacing a line break with a concat character like ;
Basically I want something like:
Hello World
My Name is Carl
I like apples
To:
"Hello World;My name is Carl;I like apples"
I'm wanting to submit it to a text column in a database and then when I retrieve the data later for viewing I want it to parse back to line-breaks using the ; as the identifier character. I'm using CodeIgniter so if there are any helper functions I can use that would be neat.
As per your requirement, you dont want to add ";" or anything. You want to show what your getting. Then use nl2br
$text = nl2br($this->input->post("textarea_name"));
store it in db and display directly without additional code.
I am trying to display a data into textarea which is fetched from tables that i have submitted via another form. The issue comes up when a new line is entered.
The data getting displayed in the textarea is as
lin1\r\nlin2
it should be like
lin1
lin2
I have tried nl2br but it does not work as expected.
How can i make things optimized. Thanks
This problem can be solved using stripcslashes() when outputting your data.
Please note that the method above is different from stripslashes() which doesn't work in this case.
I tried using nl2br but it wasn't sufficient either.
I hope str_replace saves you.
<?php
$str='lin1\r\nlin2';
$str=str_replace('\r\n','<br>',$str);
echo $str;
OUTPUT:
lin1
lin2
This is a common question and the most common answers are ln2br or str_replace.
However this is just creating unnecessary code.
In reality the problem is pretty much always that you have run the data through a mysql escape function before displaying it. Probably while you were in the process of saving it. Instead, escape the data for saving but display an unescaped version.
<?php echo str_replace('\r\n', "\r\n", $text_with_line_breaks); ?>
See single quotes & double quotes this is a trick.
A perfect solution for newbies.
you overdo quote in insert/update statement
This problem in you case you can solve doing next
<?php
$str = 'lin1\r\nlin2';
$solved_str = str_replace(array("\\r","\\n"), array("\r","\n"), $str);
var_dump($str,$solved_str);
But you need to check insert/update statement on over quotation escape symbols
I would recommend using double quotes for \r\n such as "\r\n". I've never had it work properly with single quotes.
For non- textarea use this function
function escapeNonTextarea($string){
$string=str_replace(array('\n','\r\n','\r'),array("<br>","<br","<br>"),$string);
return $string;
}
For text area use this function
function escapeTextarea($string){
$string=str_replace(array('\n','\r\n','\r'),array("\n","\r\n","\r"),$string);
return $string;
}
call appropriate function and pass argument
I have a text field that inserts its content into an SQL table. Often, I will want this content to have <p> html tags within, based on line breaks in the text field. I have tried doing a replace before inserting:
str_replace("</p><p>", "\n", $_POST["body"]);
and I have tried doing a replace with escape characters:
str_replace("</p><p>", "\n", $_POST["body"]);
with no success. Meaning they still appear as text field line breaks. There is no security issue as the field can only be accessed by an administrator. Thank you for your help.
it seems both times you are trying to replace contrary - a </p><p> to \n which obviously fails
try to swap str_replace argumants.
I don't understand what SQL has to do here though
How about replacing each separately?
str_replace(array("</p>", "<p>"), array("\n","\n"), $_POST["body"]);
I think the problem is in " latter, like google, try replacing it with ' or call function addslashes() on your text.