I want to get "0.8" from my string using PHP - php

I have a string
$string = "2.5 lakh videos views 0.8";
Now I want to get "0.8" from above string using PHP.

To get the last 3 characters:
$string = "2.5 lakh videos views 0.8";
$valSubstr = substr($string, -3);
echo $valSubstr;
To get the last "part" of the sentence. (this way you don't have to care about the count of characters in your number)
$string = "2.5 lakh videos views 0.8";
$partsString = explode(' ', $string);
$valExplode = $partsString[count($partsString) - 1];
echo $valExplode;
Both methods are bad, because you can only hope your data is in the format you expect it to be. But if your data is always in the format you showed, it would work. You can add a regex to check for unwanted characters like a trailing ".".

You can use php function substr() like this substr($string,-3);.

Related

How to eliminate\remove last character and space after that and to merge splitted word in php after using OCR for scanning documents

How to eliminate\remove last character and space after that and to merge splitted word in php after using OCR for scanning documents
Tried with rtrim, replace etc..
But it also delete - on beginning of text
$delete = array('-');
if(in_array($string[(strlen($string)-1)], $delete))
$string = substr($string, 0, strlen($string)-1);
This is an example of text after ocr scanning
'Th- is is wh- at is looking like after doc- ument is scanned
-And it not look- ing good'
You know how it should be
This is what is looking like after document ....
Like I said I tried with replace but "-" sign is also removed from begging of text...
Idea is to remove "- " (dash and space) between splitted word and to marge word again
This can be accomplished with preg_replace.
$s = 'Th- is is wh- at is looking like after doc- ument is scanned -And it not look- ing good';
$s = preg_replace('/- /','',$s);
echo preg_replace('/ -/',". -\n",$s);
This is what is looking like after document is scanned.
-And it not looking good
A string is not an array in the strict sense, but there exists in-built php functions to convert one into another. They are explode() and implode()
The code below solves your problem.
<?php
$string = "Th- is is wh- at is looking like after doc- ument is scanned -And it not look- ing good";
//$delete = array('-');
$string_array = explode('- ',$string);
$string_new = implode($string_array);
echo $string_new;

PHP strrchr parse int

I've got a problem. This is my PHP code :
$extract = $query;
$extractpoint = strrchr($extract, ".");
So, $extract is a parse_url of my website address.
Exemple : http://test.com?param.6
$extract = param.6 and $extractpoint = .6
BUT, I want a solution to have only the 6, without the point.
Can you help me with that ?
The easiest solution would be restructuring the URL. I that is not possible though you can use strpos to find the position of your specific character and then use substr to select the characters after it.
$extract = 'param.6';
echo substr($extract, strpos($extract, '.') + 1);
Demo: https://3v4l.org/CudTAG
(The +1 is because it returns the position of the match and you want to be one place past that)
There are different ways:
Filter only numbers:
$int = filter_var($extractpoint, FILTER_SANITIZE_NUMBER_INT);
Replace the point
$int = str_replace('.', '', $extractpoint)
//$int = str_replace('param.', '', $extractpoint)
Use regex
/[0-9+]/'
strrchr() results the count of the last instance of a character in a string. In order to get the next character add 1 to the count. Then use substr() to extract the next character from the string.
http://php.net/manual/en/function.strrchr.php
http://php.net/manual/en/function.substr.php

How can I get the last x characters of a string until I reach a specific character?

My questions is somewhat based off this:
How can I get the last 7 characters of a PHP string?
Mine is similar. I need to get the last x characters of a string and to stop when I reach "-"
for example, I have a booking code:
N-903
and I can get the last 3 characters like so:
$booking_Code = N-903
$booking_Code = substr($booking_Code, -3);
and the result will be:
903
This number however will increase, so I expect to see booking codes like:
N-1001
N-22520
N-201548
so the code:
substr($booking_Code, -3);
would become useless. Is there any way to use "-" as a delimiter? I think that's the correct term to use. because the number that's generated will always come after the hyphen "-". Any help would be greatly appreciated
try this
<?php
$tmpArray = explode("-",$mystr);
echo $tmpArray[1];
?>
You might want to refer to explode function in php.
As an alternative, you could also use strrchr in conjunction with the substr you have:
$booking_Code = 'N-903';
$booking_Code = substr(strrchr($booking_Code, '-'), 1);
echo $booking_Code; // 903

php - Get string before nth dash (-)

I have a string that looks something like this:
abc-def-ghi-jkl-mno-pqr-stu-vwx-yz I'd like to get the content BEFORE the 4th dash, so effectively, I'd like to get abc-def-ghi-jkl assigned to a new string, then I'd like to get mno assigned to a different string.
How could I go about doing this? I tried using explode but that changed it to an array and I didn't want to do it that way.
Try this:
$n = 4; //nth dash
$str = 'abc-def-ghi-jkl-mno-pqr-stu-vwx-yz';
$pieces = explode('-', $str);
$part1 = implode('-', array_slice($pieces, 0, $n));
$part2 = $pieces[$n];
echo $part1; //abc-def-ghi-jkl
echo $part2; //mno
See demo
http://php.net/manual/en/function.array-slice.php
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.implode.php
Can you add your source code? I done this one before but I cant remember the exact source code I used. But I am pretty sure I used explode and you can't avoid using array.
EDIT: Mark M answer is right.
you could try using substr as another possible solution
http://php.net/manual/en/function.substr.php
If I see where you are trying to get with this you could also go onto substr_replace
I guess an alternative to explode would be to find the position of the 4th - in the string and then get a substring from the start of the string up to that character.
You can find the position using a loop with the method explained at find the second occurrence of a char in a string php and then use substr(string,0,pos) to get the substring.
$string = "abc-def-ghi-jkl-mno-pqr-stu-vwx-yz";
$pos = -1;
for($i=0;$i<4;$i++)
$pos = strpos($string, '-', $pos+1);
echo substr($string, 0, $pos);
Code isn't tested but the process is easy to understand. You start at the first character (0), find a - and on the next loop you start at that position +1. The loop repeats it for a set number of times and then you get the substring from the start to that last - you found.

Restrict text input to only allow five (5) words in PHP

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

Categories