I have a simple php file with a GET from a form to throw up some files from a directory. Quite simple code but I just need to strip from the string ANY characters that are NOT lowercase alpha. How would I go about doing that? (I'm a novice).
Here 'tis:
<?php $text = $_GET['text_string']; ?>
<form method="GET" action="index.php">Please enter some letters: <input type="text" name="text_string" value=""/> and hit <input type="submit" value="enter" />
</form>
<?php /* Split characters into an array */ $array = str_split($text); ?>
<?php foreach($array as $char) : ?>
<img src="glyphs/<?php print ($char); ?>.jpg"/>
<?php endforeach ; ?>
Thanks!
To lowercase (note that you also can use mb_strtolower for better charset handling, but in this case you're only going to keep ASCII chars anyway so strtolower is enough):
$text = strtolower($text);
To remove all non-alpha chars using preg_replace:
$text = preg_replace('/[^a-z]/', '', $text);
Related
When I sanitize the input fields or text area I face a problem. When someone gave spaces and submit the form, my script accepts the form. But I want not to accept fields until there is not written at least a single character. My code is as follows.
Html
<form action="" method="POST">
<textarea name='text'></textarea>
<input type='submit' name='submit'>
</form>
Php
if(isset($_POST['submit'])){
if(isset($_POST['text']) && !empty($_POST['text'])){
//do whatever but not accept white space
}
}
You can trim whatever you want, just by using
trim()
Which removes characters from both sides of a string.
Documentaion: http://php.net/manual/bg/function.trim.php
trim and preg_replace will do this easily
<?php
echo $text = " this is niklesh raut ";
echo "\n";
$text = preg_replace('/\s+/', ' ',$text);
echo trim($text);
?>
live demo : https://eval.in/818137
OUTPUT :
this is niklesh raut
this is niklesh raut
With new line and tab : https://eval.in/818138
You can either echo out your statement:
<?php
if(isset($_POST['submit'])){
if(empty($_POST['text'])){
echo "Please enter a value.";
}
}
Or, add the required attribute to your input field.
<form action="" method="POST">
<textarea name='text' required></textarea>
<input type='submit' name='submit'>
</form>
I want <br /><br /> to turn into <br />
What's the pattern for this with regex?
Note: The <br /> tags can occur more than 2 times in a row.
$html = preg_replace('#(<br */?>\s*)+#i', '<br />', $html);
This will catch any combination of <br>, <br/>, or <br /> with any amount or type of whitespace between them and replace them with a single <br />.
You could use s/(<br \/>)+/<br \/>/, but if you are trying to use regex on HTML you are likely doing something wrong.
Edit: A slightly more robust pattern you could use if you have mixed breaks:
/(<br\ ?\/?>)+/
This will catch <br/> and <br> as well, which might be useful in certain cases.
(<br />)+
Will match them so you could use preg_replace to replace them (remember to correctly escape characters).
The answer from #FtDRbwLXw6 is great although it does not account for spaces. We can extend this regex to include that as well:
$html = preg_replace('#(<br *\/?>\s*( )*)+#', '<br>', $html);
This works fine. It works with all the combinations below.
$html = '<br><br>
Some text
<br>
<br>
Some another<br/><br>';
$html = preg_replace("/(<br.*?>\s*)+(\n)*+(<br.*?>)/","<br>",$html);
$html will be changed to
<br>
Some text
<br>
Some another<br>
I have a form to get information and when i type just spaces with no any other character it gets posted, How could i avoid inserting just spaces in the form?
<form action="index.php" method="post">
<textarea name="textA"></textarea>
<input type="submit" name="sent" value="Send">
</form>
<?php
if(isset($_POST['sent']) && !empty($_POST['textA'])){
$insert=new Insert();
$insert->insertData($_POST['textA']);
}
?>
Just use trim() which will remove all leading and trailing spaces. If there are only spaces in the string then it will become an empty string and empty() will be true.
if(isset($_POST['sent']) && !empty(trim($_POST['textA']))){
You can also do things like str_replace() to replace spaces with and empty string or preg_replace() to do the same but this should do what you need.
I have a search form, I want to $_REQUEST the search terms as an array so I can list each search term out, wrapping each term in a span for styling. How do I do that?
Edit: Here's the code requested.
<form action="http://localhost/wordpress" id="search" method="get">
<input type="text" size="30" id="s" name="s" value="Type and hit enter" onfocus="javascript:this.value='';" onblur="javascript:this.value='Type and hit enter';"/>
<br/>
<input type="submit" value="Search"/>
</form>
Update: Thanks guys for the responses. I'll use explode, it seems fairly straightforward. Plus the name sounds cool ^^
In the form:
<input type="text" name="terms[]" />
<input type="text" name="terms[]" />
<input type="text" name="terms[]" />
In the form processor:
<? foreach($_REQUEST['terms'] as $term) { ?>
<span style="searchterm"><?= htmlspecialchars($term) ?></span>
<? } ?>
If you want the user to enter multiple search terms in separate input controls, the above answers should be helpful. However, your example form leads me to wonder if you want to use only one search phrase input text box. If that's so, this might be what you're looking for:
<?php
$searchTerms = preg_split("/[\s,]+/", $_REQUEST['SearchTerms']);
foreach($searchTerms as $term) { ?>
<span class="term"><?= htmlentities($term) ?></span>
<?
}
?>
I did it this way..
Passing an array (it's really just a string to PHP):
http://www.somesite.net/myscript.php?myArray=Planes,Trains,Automobiles
Then in the script, just explode the string:
$myArray = explode(",", $_REQUEST['myArray']);
Maybe not exactly what you're looking for.
I figure you wish the user to have one single entry input, which you then wish to split into an array of separate search terms.
Split your input on whitespace (treating consecutive whitespace characters as one) to derive separate terms.
For example :
$termList = preg_split("/\s+/", trim($_REQUEST['s']));
foreach($termList as $term) { echo "<span>".htmlspecialchars($term)."</span>\n"; }
Ofcourse do not forget to properly filter and escape the input before you use it.
If you want break your search terms by space symbols just try this code:
<?php
$search_terms = explode(" ", $_REQUEST['s']);
foreach($search_terms AS $search_term_item) {
echo "<span class=\"SearchTerm\">".htmlspecialchars($search_term_item)."</span>";
}
?>
In your HTML form element you can assign the name to an array, like this:
<select id="MySelect" multiple="multiple" name="SearchTerms[]" class="MyClass">
...
</select>
then when you deal with the form after submittion you can do something like:
<?php
foreach($_REQUEST['SearchTerms'] as $SearchTerm)
{
Print("<span class=\"SearchTerm\">$SearchTerm</span>");
}
?>
Here's more details on passing in form results as an array:
http://us.php.net/manual/en/faq.html.php#faq.html.arrays
I have an input form with two textareas allowing a user to type in words separated by commas in each.
<form action="http://www.example.com/output.php" method="post">
<table border="0">
<tr>
<td>
<h3 class="title">Prefix</h3>
<textarea cols="34" rows="8" name="cat[]"></textarea></td>
<td>
<h3 class="title">Suffix</h3>
<textarea cols="34" rows="8" name="cat[]"></textarea></td>
</tr>
</table>
Enter words separated by a comma.
<input type="submit" value="Go" /> </form>
It then passes these to the output form which explodes the words from the commas and then concatenates them together until all possible permutations of the words are created. It then prints out the results into a textarea. My problem is that the output (whilst correctly formatted and have the linebreaks in between each permutation) has a br tag at the end of each line. Eg.
testtest2<br />
testtest2<br />
testtest4<br />
testetest2<br />
testetest2<br />
testetest4<br />
Output form:
$cat = $_POST['cat']; //trait set for textbox inputs
foreach(array_keys($cat) as $key){
$cat[$key] = explode(",", str_replace(' ','',$cat[$key]));
}
function showCombinations($string, $traits, $i)
{
if ($i >= count($traits))
echo trim($string)."\n";
else
{
foreach ($traits[$i] as $trait)
showCombinations("$string$trait", $traits, $i + 1);
}
}
?>
<form name=form1 method=post action=''''>
<textarea><?php ShowCombinations('', $cat, 0); ?></textarea>
</form>
When I remove the textarea tags for the output it works fine.
When I leave the textarea tags and remove/replace echo trim($string)."\n"; with "\r" or 'n' or "\n\r", the disappears but I also lose the linebreak
Replace echo trim($string)."\n"; with echo nl2br($string); then same result as 2.
Replace with echo nl2br($string)."\n"; then same result as 1.
Would appreciate any help. My noob brain is about to implode.
I'll preface this by saying I use Blogger not Wordpress but I imagine there are similarities. In Blogger there is a setting to convert line breaks into <br> tags. I guess it's convenient if you're not putting code snippets and the like in (if you're blogging about cats or something) but not so much for programming. I had to turn it off and put in my paragraph tags, etc manually.
Here is one less than ideal solution, which also mentions the issue of Wordpress inserting <br> tags in forms.
I think the <br /> tag is what you want in this case. Newline characters like \n and \r don't do anything in HTML - all whitespace, including newlines, is ignored. The <br /> tag is essentially the HTML equivalent - it stands for "break". I don't know too much about PHP specifically, so I don't know why it's putting the <br /> there for you automatically, but that's the correct HTML for what you're describing.
Technically, there are better ways to format your output then using those tags, but that's more of an advanced topic. If you are a bit new to this, like you said, then just get it working this way for now, and then maybe sometime down the road you can learn about proper semantic HTML markup, CSS styling, etc.
Try using this, I already test it...
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<?php
if(isset($_POST['status'])){
$newLineCode = "<br/>";
$message = $_POST['status'] ;
$modifiedTextAreaText = ereg_replace( "\n", $newLineCode, $message);
echo " <p>Result of Code Snippet:</p> " . $modifiedTextAreaText ;
}
?>
<form action="" method="post">
<textarea name="status"></textarea>
<input type="submit"/>
</form>
</body>
</html>
why not display the results on a <div> perhaps or a <p> ??
I believe \n should work. Perhaps you do not have or have an incorrect doctype.
Example: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
If all else, I like lock's answer. You could us a <div> and you could even style it to look similar to a <textarea> (or even better!)
It took some looking, but after some time I was able to pinpoint these two locations (lines 154 & 165 - WP 2.8) in the wp-includes/formatting.php file.
154 $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n"; //<-- before
154 $pee .= trim($tinkle, "\n") . "\n"; //<-- after
165 $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks <-- before
165 $pee = preg_replace('|(?<!<br />)\s*\n|', "\n", $pee); // optionally make line breaks <-- after
This took care of the paragraph and and break tags in my textarea field.
I solved my problem by using strip_tags function. when user would edit the past post, it was saving in the database and displaying on the blog. But strip_tags function took care of it.