Lets say I need to get a string from MySQL database smaller than 150 characters BUT I do not want to cut the last word, instead I need it until the last space and less than 150 characters.
For example:
I want:
Derrick Rose and the Chicago Bulls.
I don't want:
Derrick Rose and the Chica.
Is there a way to do this in MySQL, PHP or a combination of both?
You can do this with the built-in string functions:
reverse the first 150 characters of the string
find the first space in the reversed string
use this information to get the right string
The SQL looks something like this:
select left(left(str, 150), 150 - locate(' ', reverse(left(str, 150))))
Write a loop in PHP that starts at position 150, and works back until it encounters either a space character, or the start of the string.
If it encounters a space character, take all characters from the start of the string to the position you just found. Otherwise, use the first 150 characters (edge case that there are no space characters in the first 150).
Related
I try check a string is number or not.
number format:
2222 or 22 22 or 1 2222 or 22323423
(with space before,midlle and after digits)
Is better I use regular expression?
So how can I change it to do that?
preg_grep('~^[0-9]$~'
or Is there any faster method?
You can use this regex:
^\d+( +\d+)*$
This will allow 1 or more spaces only in the middle of the number but not at start or end.
I have a field that contains comma separated values.
I found I can verify the presence in the list of "b" using this code:
SELECT FIND_IN_SET('b','a,b,c,d,b,b')
I want a different thing:
I want to find the number of occurrences of 'b' in that comma separated list. Is this possible in MySQL? Or I must demand it to array_count_values in PHP?
This blog post seems to do what you want:
http://nostylesheets.com/2009/07/17/mysql-substr-count/
Basically, it looks at the string length of the field, removes your target sub-string, then looks at the new length. If you know your substring was 4 characters long, and your new string is now 8 characters shorter than it was, then you know you had 2 occurrences.
LOCATE should do the trick. Note that there are two different signatures - the first (two args) is to be called initially, then call the second (three args), giving the result of the first, as the third argument to the second. More info at:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_locate
LOCATE(substr,str), LOCATE(substr,str,pos)
The first syntax returns the position of the first occurrence of
substring substr in string str. The second syntax returns the position
of the first occurrence of substring substr in string str, starting at
position pos. Returns 0 if substr is not in str.
I "inherited" a buggy PHP page. I'm not an expert of this language but I think I found the origin of the bug. Inside a loop, the page sends a formatted string to the server: the string I found in the HTML page is like this one:
2011-09-19__full_1
so, it seems we have three parts:
a date (0,10);
a string (10,6);
a final number (17,1);
The code the handles this situation is the following:
$datagrid[] = array("date"=>substr($post_array_keys[$i], 0, 10),"post_mode"=>substr($post_array_keys[$i], 10, 6),"class_id"=>substr($post_array_keys[$i], 17, 1),"value"=>$_POST[$post_array_keys[$i]]);
What happens: the final number can contain more than one character, so this piece:
"class_id"=>substr($post_array_keys[$i], 17, 1)
is not correct because it seems to retrieve only one character starting from the 17th (and this seems to cause strange behaviors to the website).
Being the whole number the last part of the string, to get the entire number could I safely change this line this way?
"class_id"=>substr($post_array_keys[$i], 17, strlen($post_array_keys[$i])-17);
If you change the code the way you suggest you would get the numbers at the end starting in position 17. The original code gets only the first digit. Your code would get all the digits.
And it seems you did your homework the line
$datagrid[] = array("date"=>substr($post_array_keys[$i], 0, 10),"post_mode"=>substr($post_array_keys[$i], 10, 6),"class_id"=>substr($post_array_keys[$i], 17, 1),"value"=>$_POST[$post_array_keys[$i]]);
does give you a very good clue of what you should expect in the variable:
first 10 is the date
then you have 6 chars for post_mode
then you have 1 char for class_id
If you also confirmed that sometimes the class_id can be more than 1 char, your suggested change would give you the complete class_id at the end.
Good luck.
you could use
$array = explode("_", $string);
this functions returns an array with the elements in the string delimited by "_".
I suggest this because the double underscore may hide another value that is empty in that particular case.
If it's only the last integer causing trouble, you can use strrchr to get the "tail" of the string, starting with the last '_'.
I am using SUBSTRING function to retreive an "excerpt" of a message body:
SELECT m.id, m.thread_id, m.user_id, SUBSTRING(m.body, 1, 100) AS body, m.sent_at
FROM message m;
What I would like to do is add 3 dots to the end of the substring, but only if the source string was more than my upper limit (100 characters), i.e. if substring had to cut off the string. If the source string is less than 100 characters then no need to add any dots to the end.
I am using PHP as my scripting language.
That can be done in the query, rather than PHP, using:
SELECT m.id, m.thread_id, m.user_id,
CASE
WHEN CHAR_LENGTH(m.body) > 100 THEN CONCAT(SUBSTRING(m.body, 1, 100), '...')
ELSE m.body
END AS body,
m.sent_at
FROM MESSAGE m
The term for the three trailing dots is "ellipsis".
Ask for 101 characters. If you receive 101 characters your resource string is definitely more than 100 characters. In that case, remove the last character in your scripting language of choice and add "...". This will relieve your DB somewhat.
Personally I would advise you to create a bit of a difference though. E.g. cut off at 90 characters if and only if you exceed 110 characters (by requesting 110 + 1 characters of course). Otherwise you will get the effect I notice with Slashdot sometimes: you have a Read the rest of this comment link, only to receive the final word of the story.
More or less, the user will be annoyed if the method of retrieving the rest of the story takes more space than the story itself.
How to count characters including white space and then break after a certain length for instance how would i break a string after 25 characters onto a new line using PHP?
Fortunately somebody's already done the work. Use wordwrap.
If you really want to reinvent the wheel for learning sake, here are a few pieces to get you started:
for (...) { }
strlen()
$str[$x] to access character x of string $str
%
.
Try chunk_split() if you don't mind cutting words in half. It treats whitespace as any other char.