steve m Posted December 18, 2007 Share Posted December 18, 2007 Hello, I am kind of new to regular expressions and I've been trying to get this script to work, but it won't. I can't figure out why it doesn't work. This is just a test script, but I want the preg-match() to find "\n". The \n is posted through a form, and it seem like the preg_match() function is not reading the \ before the n, because it always comes up false. Am I doing something wrong here? I've tried several different ways to the pattern and nothing is working for me. <?php $strip_text = stripslashes($_POST["preg_text"]); if(preg_match("/(n)/i", $strip_text)) { echo "It matched"; } else { echo "Did not Match"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82226-cant-figure-why-this-doesnt-work/ Share on other sites More sharing options...
sasa Posted December 18, 2007 Share Posted December 18, 2007 try if(preg_match("/\\n/i", $strip_text)) Quote Link to comment https://forums.phpfreaks.com/topic/82226-cant-figure-why-this-doesnt-work/#findComment-417890 Share on other sites More sharing options...
steve m Posted December 18, 2007 Author Share Posted December 18, 2007 Thanks for replying back. I tried it and that didn't work either. For some reason that backslash is not being recognized literal character. Am I not escaping the backslash right? Quote Link to comment https://forums.phpfreaks.com/topic/82226-cant-figure-why-this-doesnt-work/#findComment-417936 Share on other sites More sharing options...
lemmin Posted December 18, 2007 Share Posted December 18, 2007 Are you trying to match a literal "\n" or an actual new line character? If you are trying to match a literal, you need to escape the backslash from PHP AND from regex. This means you need FOUR backslashes total. "\\\\n" will match a litteral "\n". Quote Link to comment https://forums.phpfreaks.com/topic/82226-cant-figure-why-this-doesnt-work/#findComment-417941 Share on other sites More sharing options...
steve m Posted December 18, 2007 Author Share Posted December 18, 2007 Oh sorry, I'm trying to match or replace a new line character. Would the code below work for trying to match or replace a new line character? <?php preg_match("/\n/i", $strip_text); ?> Quote Link to comment https://forums.phpfreaks.com/topic/82226-cant-figure-why-this-doesnt-work/#findComment-417945 Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 Do you want to match or replace? There really is no need for preg_match at all unless your trying to find a pattern. If your looking for a literal string just use either strpos or str_replace depending on what exactly it is your trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/82226-cant-figure-why-this-doesnt-work/#findComment-417949 Share on other sites More sharing options...
steve m Posted December 18, 2007 Author Share Posted December 18, 2007 Sorry if I'm confusing you. What I want to do is get rid of newline characters from input fields from forms. I was told to use preg_replace() to achieve that. So if I echoed the varible it still would have the "\n". Quote Link to comment https://forums.phpfreaks.com/topic/82226-cant-figure-why-this-doesnt-work/#findComment-417960 Share on other sites More sharing options...
chigley Posted December 18, 2007 Share Posted December 18, 2007 <?php $input = "Hello my name is Charlie"; $input = str_replace("\n", "", $input); echo $input; // HellomynameisCharlie ?> ^^ You mean like that? Much simpler Quote Link to comment https://forums.phpfreaks.com/topic/82226-cant-figure-why-this-doesnt-work/#findComment-417966 Share on other sites More sharing options...
lemmin Posted December 18, 2007 Share Posted December 18, 2007 Assuming you are trying to find every line, You will want to use preg_match_all: preg_match_all("[(.*)\\n]", $strip_text, $matches); That will return all the lines that have a return at the end into $matches, so maybe not the last one. Quote Link to comment https://forums.phpfreaks.com/topic/82226-cant-figure-why-this-doesnt-work/#findComment-417974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.