[Solved] string variable problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xaeus
Forum Newbie
Posts: 2
Joined: Thu Feb 02, 2006 12:16 am

[Solved] string variable problem

Post by xaeus »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


probably a stupid question but i've googled and checked what books i have and the forum and maybe i'm not searching for the right thing but this is the problem i'm having

i'm writing a simple php script that takes input from a textarea form, passes it to a string variable, then i pull out what i don't want of the string using regex. (i know the regex isn't as elegant as it can be, i just tossed it together to get something working). 

<form action="strip.php" method="post">
<textarea name="text" cols=70 rows=50></textarea>



--strip.php follows:

Code: Select all

<?php

$text_data = $_POST['text'];

print($text_data . "<br><br>");

if (strlen($text_data) > 0)
{
	$new_data = eregi_replace("[\[0-9]+[0-9]+:[0-9]+[0-9]+:[0-9]+[0-9]+]\ ","",$text_data);
	print($new_data);
}else{
	print("Invalid Entry. Try Again.");
}
	
?>

now, let's say for instance the input sitting in the textarea looks like

Code: Select all

[00:00:01] garbage
[00:00:02] in
[00:00:03] garbage
[00:00:04] out
after the action is fired, and the php script stips off the times, it SHOULD look like this:

Code: Select all

garbage
in
garbage
out
but, alas, it doesn't...i have the script display the text before and after parsing out the times and both of them look like this:

Code: Select all

[00:00:01] garbage [00:00:02] in [00:00:03] garbage [00:00:04] out

garbage in garbage out

am i missing something obvious? it seems as if the string variable doesn't hold the carriage returns? what can i do to fix this


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Before...

Code: Select all

print($text_data . "<br><br>");
add line...

Code: Select all

$text_data = str_replace("\n", "<br>", $text_data");
There are problems with the php tag it is adding

Code: Select all

nt color="#000000"><?php
To the beginning of the line.

Let me enter it using the code tag...

Code: Select all

print($text_data . "<br><br>");
add line...

Code: Select all

$text_data = str_replace("\n", "<br>", $text_data");
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. use preg_replace() instead of eregi_replace(): runs faster and has more flexibility.
  2. Your regex pattern has a logic error. Move the first occurance of a backslash to before the first opening bracket.
xaeus
Forum Newbie
Posts: 2
Joined: Thu Feb 02, 2006 12:16 am

Post by xaeus »

worked like a charm, thanks for the super fast response :D

i do apologize for forgetting the proper tags :oops:
Post Reply