php string to int

php string to int

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Mechanics of PHP String Casting to Integers

When you cast a string to an integer in PHP using (int) or intval(), the language does not parse the entire character sequence. Instead, it adheres to a specific rule: conversion stops at the first non-numeric character.

In your example:

$a = '88';
$b = '88 8888';

echo (int)$a; // Outputs: 88
echo (int)$b; // Outputs: 88

The output for $b is 88, not 888888. This happens because PHP identifies the space character immediately following the first two digits. Once it encounters that space, it truncates the conversion process. The remainder of the string, '8888', is completely ignored during the cast operation.

There is no built-in function in standard PHP that concatenates or interprets split numbers within a single string to form a larger integer automatically. If you run intval('88 8888') or cast it directly, the result will always be based on the leading numeric portion. To achieve the value 888888, you must manually manipulate the string before casting.

Removing Delimiters to Form a Single Number

The most straightforward approach to transform '88 8888' into 888888 is to strip out the non-numeric characters. You can achieve this using str_replace, preg_replace, or by leveraging regular expressions more broadly.

Using str_replace is simple if you know the specific delimiter:

$b = '88 8888';
$cleaned = str_replace(' ', '', $b);
$result = (int)$cleaned; // Outputs: 888888

This works well for spaces, but it becomes fragile if the input uses tabs, newlines, or other whitespace characters. A more robust solution involves removing all non-digit characters.

You can use a regular expression to filter out anything that isn't a number:

$b = '88 8888';
$cleaned = preg_replace('/[^0-9]/', '', $b);
$result = (int)$cleaned; // Outputs: 888888

This pattern /[^0-9]/ matches any character that is not a digit from 0 to 9 and replaces it with an empty string. This handles spaces, tabs, newlines, and even negative signs or decimal points if you want to be strict about integers only. Note that if your string contains a minus sign like '88 -8888', this specific pattern will remove the sign, resulting in 888888 rather than -888888. If preserving negative signs is important, adjust the regex to /[^0-9\-]/.

Handling Negative Signs and Decimals

A critical pitfall arises when dealing with strings that contain negative numbers or decimal points. PHP's casting behavior treats the first non-digit character as the truncation point unless it is a leading minus sign or a decimal point followed by digits.

Consider the string '-88 8888'.

$b = '-88 8888';
echo (int)$b; // Outputs: -88

The space truncates the value immediately after -88. If you use the regex /[^0-9]/ to clean it, the minus sign is removed, and you get a positive 888888. This might not be the desired outcome if the input represents a negative integer.

To preserve the sign while removing spaces, you must allow the minus sign in your replacement pattern:

$b = '-88 8888';
$cleaned = preg_replace('/[^0-9\-]/', '', $b);
$result = (int)$cleaned; // Outputs: -888888

However, you must be cautious with decimal points. If your input is '88.5 8888', the dot is a valid character in floating-point numbers but invalid for integers.

$b = '88.5 8888';
$cleaned = preg_replace('/[^0-9]/', '', $b); // Removes the dot
$result = (int)$cleaned; // Outputs: 8858888

In this scenario, removing the decimal point merges the integer part with the fractional part, which creates a new number entirely different from the original components. If your goal is strictly to merge numeric segments regardless of their mathematical validity, stripping non-digits (and optional signs) is the correct path.

When to Use This Approach

Before implementing custom string cleaning logic, ask yourself if you are solving the right problem. Often, receiving a string like '88 8888' and expecting 888888 implies that the data format is inconsistent or malformed. In production environments, relying on concatenated numbers from single strings can introduce significant bugs.

If you are processing user input or API responses, it is usually better to enforce strict validation rules rather than cleaning up dirty data post-receipt. If the source system sends '88 8888' when it should send 888888, fixing the regex in your application only masks the root cause.

That said, if you are parsing legacy logs or handling specific file formats where numbers are tokenized by spaces within a single line, the preg_replace method provides a reliable way to reconstruct the integer value without writing complex parsing loops.

For developers using the Laravel framework, while there isn't a dedicated helper specifically for this string-to-int concatenation issue, you can easily wrap this logic in a custom helper function or use a package like tymon/jwt-auth if you are dealing with tokenized numbers in security contexts. However, standard PHP functions remain the most performant choice for simple string manipulation tasks.

Ultimately, the behavior of (int) is designed for efficiency: it reads from left to right until it hits a wall. To get past that wall, you must remove the wall itself.

Our network