w3resource

PHP: chunk_split() function

Description

The chunk_split() function is used to split a string into a series of smaller chunks.

Version:

(PHP 4 and above)

Syntax:

chunk_split(string_name, string_length, end_string) 

Parameters:

Name Description Required /
Optional
Type
string_name The string to be split. Required String
string_length The chunk length. Optional Integer
end_string The line ending sequence. Optional String

Return values

The extracted string.

Value Type: String.

Pictorial Presentation

php-string-chunk_split()

Example:

<?php
$string_name1='w3resource.com';
echo chunk_split($string_name1,1,".");
echo '<br>';
echo chunk_split($string_name1,2,"/");
echo '<br>';
echo chunk_split($string_name1,1,"--");
?>

Output:

w.3.r.e.s.o.u.r.c.e...c.o.m.
w3/re/so/ur/ce/.c/om/
w--3--r--e--s--o--u--r--c--e--.--c--o--m--

View the example in the browser

See also

PHP Function Reference

Previous: chr
Next: convert_cyr_string



PHP: Tips of the Day

Get the full URL in PHP

Have a look at $_SERVER['REQUEST_URI'], i.e.

Example:

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

(Note that the double quoted string syntax is perfectly correct)

If you want to support both HTTP and HTTPS, you can use

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.

Ref : https://bit.ly/2ZYItNJ