EchoFool Posted October 8, 2009 Share Posted October 8, 2009 Hey, I am wondering how i could manipulate a string of urls and seperate them regardless of characters inbetween ? For example: http://www.phpfreaks.comhttp://www.phpfreaks.com Would become: http://www.phpfreaks.com http://www.phpfreaks.com (one on each line) Now as the url could have GETs on them, the only way to make sure it didnt mess up was to seperate from "http://wwww." But i don't get how i could do that =/ Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/ Share on other sites More sharing options...
cags Posted October 8, 2009 Share Posted October 8, 2009 Is the input string only going to contain urls and will they always be 'touching' in the manner of the example? Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933265 Share on other sites More sharing options...
EchoFool Posted October 8, 2009 Author Share Posted October 8, 2009 No they won't always be touching... the text area has "one url per line" but some noobs ignore this format rule so i want php to sort it for them if they too lazy. And yes it will only contain urls. Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933269 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 will urls always start with http:// ? if so you could do an explode on the string, and then use an array_map to put the http:// back on if you wanted, and do an implode with the line break tag Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933276 Share on other sites More sharing options...
cags Posted October 8, 2009 Share Posted October 8, 2009 The only ways I can think of doing it feel like really awkward 'hacks. You could split the string on the newline character using explode() then loop through the values checking if they contain http:// twice and split them if they do. EDIT: d'oh Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933277 Share on other sites More sharing options...
EchoFool Posted October 8, 2009 Author Share Posted October 8, 2009 will urls always start with http:// ? if so you could do an explode on the string, and then use an array_map to put the http:// back on if you wanted, and do an implode with the line break tag Well thats the thing some people only put www. others put http... so i need to cover both really =/ Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933284 Share on other sites More sharing options...
cags Posted October 8, 2009 Share Posted October 8, 2009 will they all put www.example.com or http://www.example.com or are some likely to be http://example.com? Or even just example.com? Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933289 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 Well, if its guarenteed that each will start with www, then you could just do an explode on www, do an array map to remove every instance of http, then do another array_map to add http://www to the beginning of each entry. then an implode to add the line breaks. if you give me some test data I can try to whip up a function that will do that for you Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933291 Share on other sites More sharing options...
EchoFool Posted October 9, 2009 Author Share Posted October 9, 2009 Well users might put: www. http://domain.com or http://www. So it would need to cater all 3 varieties. Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933421 Share on other sites More sharing options...
cags Posted October 9, 2009 Share Posted October 9, 2009 Why not provide an array of text input boxes rather than a text area, simple button at the bottom if they wish to add any more sites, then you don't have to worry about it.... Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933428 Share on other sites More sharing options...
EchoFool Posted October 12, 2009 Author Share Posted October 12, 2009 Why not provide an array of text input boxes rather than a text area, simple button at the bottom if they wish to add any more sites, then you don't have to worry about it.... Could you elaborate i don't think i understand you? Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935600 Share on other sites More sharing options...
mikesta707 Posted October 12, 2009 Share Posted October 12, 2009 create text boxes instead of a text area, like code <input type="text" name="sites[]" /> <input type="text" name="sites[]" /> <input type="text" name="sites[]" /> and you could have a javascript button that could dynamically add input boxes as needed, and use that Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935603 Share on other sites More sharing options...
cags Posted October 12, 2009 Share Posted October 12, 2009 <input type="text" name="urls[]" /> <input type="text" name="urls[]" /> <input type="text" name="urls[]" /> <input type="text" name="urls[]" /> <input type="button" name="add" /> You then use a bit of JavaScript to dynamically add in a new textbox if they have any more URLs to add. Edit: beaten to it. Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935605 Share on other sites More sharing options...
EchoFool Posted October 12, 2009 Author Share Posted October 12, 2009 Thing is people have Js turned off so php was my preferred choice.. Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935620 Share on other sites More sharing options...
cags Posted October 12, 2009 Share Posted October 12, 2009 Technically you could do it with PHP it would just be abit more work as you'd have to re-submit and keep track of everything. It would be probably easier to make a regular expression, but I'm buggered if I can work out an approriate one, which was the advantage of using seperate boxes, you wouldn't need to parse it. Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935680 Share on other sites More sharing options...
cags Posted October 12, 2009 Share Posted October 12, 2009 I've been trying to work one out that basically searches for strings that consist of... "http://www.", "www." or "http://" followed by any valid character in a URL which is followed by either "http://www.", "www.", "http://" or a whitespace character. But it doesn't seem very simple. I was hoping it would be as simple as something along the lines of... (((http://www\.)|(http://)|(www\.)).+?)((http://www\.)|(http://)|(www\.)|\s) ... but I've decided it's beyond my current knowledge of Regular Expressions. Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935699 Share on other sites More sharing options...
EchoFool Posted October 14, 2009 Author Share Posted October 14, 2009 Thanks for giving it a try though! Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-936520 Share on other sites More sharing options...
salathe Posted October 15, 2009 Share Posted October 15, 2009 If your input will only contain URLs then the following regular expression should help (or might guide you in a certain direction at least). #.+?(?=http://|(?<!http://)www\.|$)#m An example of its use: $subject = ' www.example.comhttp://www.example.com http://example.com?foo=bar http://www.phpfreaks.comhttp://www.phpfreaks.com '; $pattern = '#.+?(?=http://|(?<!http://)www\.|$)#m'; preg_match_all($pattern, $subject, $matches); print_r($matches); Output: Array ( [0] => Array ( [0] => www.example.com [1] => http://www.example.com [2] => http://example.com?foo=bar [3] => http://www.phpfreaks.com [4] => http://www.phpfreaks.com ) ) Caveat: this will capture anything, it does not look explicitly for URLs. If your string contains other things, it will grab those as well. However, the OP clearly states the subject will be a "string of urls". Quote Link to comment https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-937457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.