jimmi8 Posted February 5, 2007 Share Posted February 5, 2007 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 More sharing options...
snakebit Posted February 5, 2007 Share Posted February 5, 2007 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. Link to comment https://forums.phpfreaks.com/topic/37134-formatting-a-url-string/#findComment-177337 Share on other sites More sharing options...
jimmi8 Posted February 5, 2007 Author Share Posted February 5, 2007 aha fantastic! Now the only thing i cant fathom out is how to search use this to search my page. I dont know exactly where someone will be inputting a link Link to comment https://forums.phpfreaks.com/topic/37134-formatting-a-url-string/#findComment-177342 Share on other sites More sharing options...
snakebit Posted February 5, 2007 Share Posted February 5, 2007 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. Link to comment https://forums.phpfreaks.com/topic/37134-formatting-a-url-string/#findComment-177450 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.