PHP convert long URL (string) in multi line

I wanna to show a long string in a cell in a table. But the URL is too long. How I can convert something like this:

/vlive.qqvideo.tc.qq.com/u0020mkrnds.p1203.1.mp4?vkey=7AB139BF6B32F537 47E8FF192E6FE557B3A3D644C034E34BF6EAEB4E0774F2A92EF3AC5C007520BB925E5C 8A18E6D302C2DAE0A295B26AA8FD1DC8069D47CE1B4A16A56870BD1ACA3E86ABE4C079 659DB2182FC71217AB68CCD344CE65694457E3F53549CD617D5C9F671A26C70DC68F93 F1D7BCD017762F&guid=F5EB01CC01A8E08CD83630828DE17C2B02162FD8&locid=a06 f98fd-fa26-44e5-acc5-0d83f9df03af&s ize=9418655&ocid=253564332 

To

/vlive.qqvideo.tc.qq.com/u0020mkrnds.p1203.1.mp4?vkey=7AB139BF6B32F537 
47E8FF192E6FE557B3A3D644C034E34BF6EAEB4E0774F2A92EF3AC5C007520BB925E5C 
8A18E6D302C2DAE0A295B26AA8FD1DC8069D47CE1B4A16A56870BD1ACA3E86ABE4C079 
659DB2182FC71217AB68CCD344CE65694457E3F53549CD617D5C9F671A26C70DC68F93 
F1D7BCD017762F&guid=F5EB01CC01A8E08CD83630828DE17C2B02162FD8&locid=a06
f98fd-fa26-44e5-acc5-0d83f9df03af&size=9418655&ocid=253564332 

I’ve read many posts which were about splitting based on words not letters. As you can see, we have one long word/string in the URL.

Any other methods to shorten this URL without delete any information on it will be appreciated.

Answer

Use chunk_split

Can be used to split a string into smaller chunks which is useful for e.g. converting base64_encode() output to match RFC 2045 semantics. It inserts separator every length characters.

<?php

$data = '/vlive.qqvideo.tc.qq.com/u0020mkrnds.p1203.1.mp4?vkey=7AB139BF6B32F537 47E8FF192E6FE557B3A3D644C034E34BF6EAEB4E0774F2A92EF3AC5C007520BB925E5C 8A18E6D302C2DAE0A295B26AA8FD1DC8069D47CE1B4A16A56870BD1ACA3E86ABE4C079 659DB2182FC71217AB68CCD344CE65694457E3F53549CD617D5C9F671A26C70DC68F93 F1D7BCD017762F&guid=F5EB01CC01A8E08CD83630828DE17C2B02162FD8&locid=a06 f98fd-fa26-44e5-acc5-0d83f9df03af&s ize=9418655&ocid=253564332 ';


echo chunk_split($data, 20, '<br>');

The above code would insert a line-break every 20 chars.

Since you’re using the data in a htlm table, I would recomend using <br> as line-break.

Try it online!

output (See <br>s after each 20 chars):

/vlive.qqvideo.tc.qq<br>.com/u0020mkrnds.p12<br>03.1.mp4?vkey=7AB139<br>BF6B32F537 47E8FF192<br>E6FE557B3A3D644C034E<br>34BF6EAEB4E0774F2A92<br>EF3AC5C007520BB925E5<br>C 8A18E6D302C2DAE0A2<br>95B26AA8FD1DC8069D47<br>CE1B4A16A56870BD1ACA<br>3E86ABE4C079 659DB21<br>82FC71217AB68CCD344C<br>E65694457E3F53549CD6<br>17D5C9F671A26C70DC68<br>F93 F1D7BCD017762F&g<br>uid=F5EB01CC01A8E08C<br>D83630828DE17C2B0216<br>2FD8&locid=a06 f98fd<br>-fa26-44e5-acc5-0d83<br>f9df03af&s ize=94186<br>55&ocid=253564332 <br>

Leave a Reply

Your email address will not be published. Required fields are marked *