Jump to content

formatting a url string


jimmi8

Recommended Posts

hi,

i want to search a page for any string beginning with http or www and the count how many characters in that string. If theres more than thirty characters i want to put anything after this 30 chars on to a new line. Some of the urls being posted to my site are too long and cross in to another column.

 

I know this would involve a regular expression and using the substring function but i havent got a clue how to begin. Could anyone point me in the right direction?

Link to comment
https://forums.phpfreaks.com/topic/37134-formatting-a-url-string/
Share on other sites

You can use

preg_match_all ( string pattern, string subject, array &matches )

For pattern use this string 'http://[w]{0,3}[A-Za-z0-9/.-]+', It will return all strings which are in following formats: http://domainname.com/sdsds.test and http://www.domainname.com/sdsds.test

For subject write your text. All matches will be put in array.

More info you can find in php manual. ;)

Hmm... For example you have 3 fields and you POST this fields to the php script there you can make this:

 

$fieldsArr=$_POST //I don't know how secure is that 
foreach($fieldsArr as $value){
preg_match_all ('\http://[w]{0,3}[A-Za-z0-9/.-]+\', $value, $matches);
}
print_r($matches);

I'm not quite sure, ;) after the for loop end, you will have all links from all fields in $matches array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.