Jump to content

Recommended Posts

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 =/

Link to comment
https://forums.phpfreaks.com/topic/177007-string-manipulation/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933277
Share on other sites

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 =/

Link to comment
https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933284
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-933291
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935603
Share on other sites

<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.

Link to comment
https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935605
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935680
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-935699
Share on other sites

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".

Link to comment
https://forums.phpfreaks.com/topic/177007-string-manipulation/#findComment-937457
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.