I want to insert a text string inside another one after certain number or words. For example:
$text_to_add = "#Some Text to Add#";
$text_original = "This is the complete text in which I want to insert the other text";
$number_words = 7; // Variable that will define after how many words I must put $text_to_add
I want to get the following result when I print $text_original:
This is the complete text in which #Some Text to Add# I want to insert the other text
I can use this function http://php.net/manual/en/function.str-word-count.php to get an array of words, go through it building a new string with the $text_to_add inserted, but I wondering if there is a better way to accomplish this, since some of my $text_original texts are very long.
Thanks in advance.
Try this:
$arr = explode(" ",$text,$number_words+1);
$last = array_pop($arr);
return implode(" ",$arr)." ".$text_to_add." ".$last;
Related
I have a string: /userPosts/hemlata993/20
I want to remove this /userPosts/hemlata993/.
I checked some answers but not being able to remove the first part. How can I do that? I am using php.
$string = /userPosts/hemlata993/20
I want the output as 20 because 20 is the directory or file name that I want to get
You can do it as follows:
$p = basename(parse_url("/userPosts/hemlata993/20")['path']);
echo $p; //20
If this is what you want and the format of the string is always going to be like the one that you provided, this will work:
$string = "/userPosts/hemlata993/20";
$string_arr = (explode("/",$string));
echo $string_arr[3];
I am working with php. I have some dynamic string. Now I want to add some number after some string. Like, I have a string this is me (1). Now I want to add -7 after 1. So that string should be print like this this is me (1-7).
I have done this properly by using substr_replace. like this
substr_replace('this is me (1)','-59',-1,-1)
Now if there is more than one number like this this is me(2,3,1). I want to add -7 after each number. like this one this is me(2-7,3-7,1-7).
Please help. TIA
I dont know if there is a good way to do this in one or two lines, but the solution I came up with looks something like this:
$subject = "this is me (2,3,1)";
if (preg_match('[(?<text>.*)\((?<numbers>[0-9,]+)\)]', $subject, $matches)) {
$numbers = explode(",", $matches['numbers']);
$numbers = array_map(function($item) {
return $item.'-7';
}, $numbers);
echo $matches['text'].'('.implode(",", $numbers).')';
}
What happens here is the following:
preg_match checks whether the text is in our desired format
We generate an array from our captured named group numbers with explode
We add our "Magic Value" (-7) to every array element
We're joining the text back together
I have list in html text arealike this.
12345
23456
12345
78938
85768
my question, how to get the list and create new array with list format..?
sorry about my english
It's unclear what you mean, exactly, but I am assuming you have a list of numbers, separated by line breaks. In that case, you can do this:
explode("\n", $the_string);
If you need to strip out carriage returns (like on Windows), do this:
explode("\n", preg_replace("/\r/", "", $the_string));
jQuery : Get textarea value and replace new line with comma
var txtval = $.trim($("#txtareaid").val()).replace(/\r?\n/g, ',');
// PASS jQuery variable to PHP
PHP: explode() function to convert string into an array based on comma
$a = txtval;
print_r(explode(',', $a));
First You want to Create Array Then,
Use it
$arr1 = array("12345","23456","12345");
echo "I want need".$arr1[0]."";
Say you submit the form to 1.php
code(1.php)
$text=$_REQUEST['t1'];
$arr=explode("\n", trim($text));//$arr is the array of all the entries in the textarea with name 't1'
I want to know how I can allow only five (5) words on text input using PHP.
I know that I can use the strlen function for character count, but I was wondering how I can do it for words.
You can try it like this:
$string = "this has way more than 5 words so we want to deny it ";
//edit: make sure only one space separates words if we want to get really robust:
//(found this regex through a search and havent tested it)
$string = preg_replace("/\\s+/", " ", $string);
//trim off beginning and end spaces;
$string = trim($string);
//get an array of the words
$wordArray = explode(" ", $string);
//get the word count
$wordCount = sizeof($wordArray);
//see if its too big
if($wordCount > 5) echo "Please make a shorter string";
should work :-)
If you do;
substr_count($_POST['your text box'], ' ');
And limit it to 4
If $input is your input string,
$wordArray = explode(' ', $input);
if (count($wordArray) > 5)
//do something; too many words
Although I honestly don't know why you'd want to do your input validation with php. If you just use javascript, you can give the user a chance to correct the input before the form submits.
Aside from all these nice solutions using explode() or substr_count(), why not simply use PHP's built-in function to count the number of words in a string. I know the function name isn't particularly intuitive for this purpose, but:
$wordCount = str_word_count($string);
would be my suggestion.
Note, this isn't necessarily quite as effective when using multibyte character sets. In that case, something like:
define("WORD_COUNT_MASK", "/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u");
function str_word_count_utf8($str)
{
return preg_match_all(WORD_COUNT_MASK, $str, $matches);
}
is suggested on the str_word_count() manual page
You will have to do it twice, once using JavaScript at the client-side and then using PHP at the server-side.
In PHP, use split function to split it by space.So you will get the words in an array. Then check the length of the array.
$mytextboxcontent=$_GET["txtContent"];
$words = explode(" ", $mytextboxcontent);
$numberOfWords=count($words);
if($numberOfWords>5)
{
echo "Only 5 words allowed";
}
else
{
//do whatever you want....
}
I didn't test this.Hope this works. I don't have a PHP environment set up on my machine now.
You could count the number of spaces...
$wordCount = substr_count($input, ' ');
i think you want to do it first with Javascript to only allow the user to insert 5 words (and after validate it with PHP to avoid bad intentions)
In JS you need to count the chars as you type them and keep the count of the words you write ( by incrementing the counter each space)
Take a look of this code: http://javascript.internet.com/forms/limit-characters-and-words-entered.html
I have a field that is in this format
5551112391^HUMAN^HUMAN-800-800^6-main^^
How would I only grab the numbers 5551112391 before the character ^?
Would you do this with regex?
You can make use of explode:
$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
$arr = explode('^',$var);
$num = $arr[0];
Using regex:
$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
if(preg_match('/^(\d+)/',trim($var),$m)){
$num = $m[1];
}
Regex overkill, nice...
What about simple cast to int? Will work perfectly OK if the number is in the beginning of data. And definitely faster than regexps...
$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
$num = (int)$var;
http://www.php.net/manual/en/language.types.type-juggling.php
You're doing it in completely wrong way.
You treat mysql database as a flat text file. But it is not.
All these fields must be separated and stored in separate columns.
To get only certain data from the table, you should not select all rows and then compare one by one but make database do it for you:
SELECT * FROM table WHERE number=5551112391