Retrieving the First n letters from table in sql [duplicate] - php

This question already has answers here:
Truncate a string to first n characters of a string and add three dots if any characters are removed
(20 answers)
Closed 7 years ago.
I saw this post which had my ans: MySQL Select Query - Get only first 10 characters of a value
How to do the same using php ?

This can be done by substr function. Following is the working example with code.
http://sugunan.net/demo/substr.php
<?php
$full_str = "Hello, this is my subject and how are you";
$firstn = substr ( $full_str , 0 ,10 );
echo $firstn;
?>

Use the substr() function. Read about it here http://php.net/manual/en/function.substr.php
$shortString = substr($longString,$startPoint,$numberOfCharactersToReturn);
eg: substr('abcdefg',0,3);
// returns 'abc'

Related

How to count the number of times a "number" appears in string using php? [duplicate]

This question already has answers here:
How to check how many times something occurs in a string in PHP
(5 answers)
Extract a single (unsigned) integer from a string
(23 answers)
Closed 4 years ago.
I am trying to count how many times number appears in a string. For example, in the string "rt_876_io_542_po_367" there are three numbers and in the string "tr_766_ 756" there are two numbers. How do I do this with PHP?? I tried the following code:
$str="RT_657_YT_89";
$key=preg_match_all('!/_[0-9]_/!',$str);
echo $key;
but it echos "0"!! please help
This can easily be accomplished with a regular expression.
function countDigits( $str )
{
return preg_match_all( "/[0-9]/", $str );
}
The function will return the amount of times the pattern was found, which in this case is any digit.

Getting the integer from a string in form of a comma separated/decorated number? [duplicate]

This question already has answers here:
php converting formatted int to int
(2 answers)
Closed 6 years ago.
I am parsing a stream of strings like '1,985' and I want to convert them to the respective integer, like 1985 in this case.
Is there any built-in/efficient PHP function which does that?
Use intval and str_replace for getting your desire result. Intval for your integer number and str_replace for remove the comma from string.
$str = '1,985';
var_dump(intval(str_replace(",", "", $str))); //int(1985)

Using PHP to find the number inside {{2461245}} [duplicate]

This question already has answers here:
PHP Extract numbers from a string
(8 answers)
Closed 7 years ago.
What is the best way to extract the number in the following string:
"This is a test string {2461245} and I need to extract the number"
Thank you.
This should help you :
$myText = 'This is a test string {2461245} and I need to extract the number';
preg_replace("/[^0-9]/","",$myText);
What you are saying there is : I want to replace everything which is not a number in the myText variable by "", which means everything which will stay in the end is your number.

php remove special characters [duplicate]

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 7 years ago.
I have a string like this in the database
!80!n!98!n!22!n!6!n!76!n!1!n!24!n!129!n!59!n!73!n!7!n!40!n!85!n!35!n!42!n!126!n!81!n!37!n!128!n!147!n!106!n
I want to remove those ! and n symbols and want to add a pipe in between characters
80|98|22|6|76|1|24|129|59|73|7|40|85|35|42|126|81|37|128|147|106
you could update the column first:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, '!n!', '|')
WHERE SomeOtherColumn LIKE '%PATTERN%'
More information on replace here:
How can I use mySQL replace() to replace strings in multiple records?
<?php
$str="!80!n!98!n!22!n!6!n!76!n!1!n!24!n!129!n!59!n!73!n!7!n!40!n!85!n!35!n!42!n!126!n!81!n!37!n!128!n!147!n!106!n
";
$str1 = preg_replace('/\s+/','|',str_replace(array("!n","!","!n!"),' ',trim($str)));
echo $output = substr($str1, 1, -1);
OUTPUT
80|98|22|6|76|1|24|129|59|73|7|40|85|35|42|126|81|37|128|147|106

How to find 3 or more of a character in a string? [duplicate]

This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Closed 9 years ago.
$string = "oidjdssd , odi,jdois, 3089u,, oisdjsd";
How do i find out if theres more than 3 commas in the string above in the best way?
I would suggest substr_count. You can see if the result is >3 to see if there's more than three.
echo count_chars($string)[ord(',')];
Or for PHP<5.4
$chars = count_chars($string);
echo $chars[ord(',')];
BTW: As it seems, that you are handling CSV-data, you should have a look at str_getcsv()

Categories