get the value of combined string and integer separately - php

I got this result from mysql query using php:
chris123
I want to separate chris from 123 because I am going to use the value of int for operation.
$char=string('chris123');
$int=int('chris123');
How am I able to do it in PHP? Not PDO. Not so familiar with it. Thanks.

If you know the non-number parts are always going to be lowercase characters, you can do this:
$str = 'chris123';
$num = trim($str, 'a..z');
If you also want it to be an actual integer, you can cast it:
$str = 'chris123';
$num = (int) trim($str, 'a..z');

Related

How to change integer value in preg_match PHP?

sorry if my question was stupid, please someone help me to fix this issue.
i have string like
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
this $str_value is dynamic , it will change each page. now i need to replace 9 in this string as 10. add integer 1 and replace
for example if the $str_value = "http://99.99.99.99/var/test/src/158-of-box.html/251/"
then output should be
http://99.99.99.99/var/test/src/158-of-box.html/252/
i tried to replace using preg_match but i m getting wrong please somesone help me
$str = preg_replace('/[\/\d+\/]/', '10',$str_value );
$str = preg_replace('/[\/\d+\/]/', '[\/\d+\/]+1',$str_value );
Thank's for the answer, #Calimero! You've been faster than me, but I would like to post my answer, too ;-)
Another possibilty is to fetch the integer by using a group. So you don't need to trim $matches[0] to remove the slashes.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$str = preg_replace_callback('/\/([\d+])\//', function($matches) {
return '/'.($matches[1]+1).'/';
}, $str_value);
echo $str;
You need to use a callback to increment the value, it cannot be done directly in the regular expression itself, like so :
$lnk= "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$lnk= preg_replace_callback("#/\\d+/#",function($matches){return "/".(trim($matches[0],"/")+1)."/";},$lnk); // http://99.99.99.99/var/test/src/158-of-box.html/10/
Basically, the regexp will capture a pure integer number enclosed by slashes, pass it along to the callback function which will purge the integer value, increment it, then return it for replacement with padded slashes on each side.
I'd suggest also another approach based on explode and implode instead of doing any regexp stuff. In my opinion this is more readable.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/11/";
// explode the initial value by '/'
$explodedArray = explode('/', $str_value);
// get the position of the page number
$targetIndex = count($explodedArray) - 2;
// increment the value
$explodedArray[$targetIndex]++;
// implode back the original string
$new_str_value = implode('/', $explodedArray);

Remove certain character of string PHP

I need to remove the third character of a string. Here's what I tried.
$my_string['2'] = "";
This worked find, but when I tried to put this variable in a MySQL query, it returned errors.
What is the best way to remove a certain character (character 3 in my case) from a string?
Try out substr_replace.
For example:
$new_string = substr_replace($my_string, '', 2, 1);
Use an integer as index, not a string:
$my_string[2] = '';

PHP manipulate string (find/replace if component present)

I need to check a string using PHP for a particular text component and replace the value if it is present with a new value. For example I might have a string like this:
?customerID=12345&recid=65&skip=20&type=job
I need to locate the "&skip=20" (it won't always = 20, it could be any number like 20, 80, 100, etc) substring and replace the "20" value with a new value from a variable ($newValue). So if that string was present and $newValue = 40 the new string would be:
?customerID=12345&recid=65&skip=40&type=job
If the string was:
?customerID=12345&recid=65&skip=160&type=job
the new string would be ($newValue = 180):
?customerID=12345&recid=65&skip=180&type=job
I'm pretty sure I should be using these functions - strpos, preg_match, preg_replace - but I've spent way too long on this and don't appear to be getting any closer. If anyone can show me how to use these or other functions to find the substring and replace the value that would be much appreciated.
Try preg_replace_callback
<?php
$str = "?customerID=12345&recid=65&skip=20&type=job";
$newStr = preg_replace_callback("/skip=([0-9]+)/i", "doIt", $str)
function doIt($match){
return $match[1] + 20;
}

PHP -- Convert string to bigint

I have the following string which i need to convert to integer or bigint.
$test="99999977706";
I tried:
echo (int)$test;
echo (integer)$test;
echo intval($test);
But they are all returning me 2147483647.
How do i convert the above string to a number or bigint?
Many many thanks for all suggestions.
MySQL isn't going to know the difference. Just feed the string into your query as though it is a number without first type casting it in PHP.
For example:
$SQL = "SELECT * FROM table WHERE id = $test";
working solution :
<?php
$str = "99999977706";
$bigInt = gmp_init($str);
$bigIntVal = gmp_intval($bigInt);
echo $bigIntVal;
?>
It will return : 99999977706
In your snippet $test is already a number, more specifically a float. PHP will convert the contents of $test to the correct type when you use it as a number or a string, because the language is loosely typed.
For arbitrary length integers use GMP.
You can treat the string as string without converting it.
Just use a regex to leave only numbers:
$test = "99999977706";
$test = preg_replace('/[^0-9]/', '', $test);
It can help you,
$token= sprintf("%.0f",$appdata['access_token'] );
you can refer with this link

getting only certain data from table

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

Categories