josafa Posted July 26, 2008 Share Posted July 26, 2008 As transform this string "house, place, free," in this "<a href="url=house">house</a> <a href="url=place">place</a>" <a href="url=free">free</a>" Link to comment https://forums.phpfreaks.com/topic/116732-transform-string/ Share on other sites More sharing options...
MFHJoe Posted July 26, 2008 Share Posted July 26, 2008 <?php // Your string of words $str = "house, place, free,"; // Separate them at the commas, into an array $str = explode(',', $str); // Set the $html variable so we can add to it with the following foreach $html = ''; foreach($str as $val) { // EDIT: Trim $val so spaces don't matter $val = trim($val); $html .= '<a href="url='.$val.'">'.$val.'</a> '; } echo $html; ?> And you can add to that string as much as you want, as long as the words are separated by commas. Link to comment https://forums.phpfreaks.com/topic/116732-transform-string/#findComment-600233 Share on other sites More sharing options...
DarkWater Posted July 26, 2008 Share Posted July 26, 2008 Comma and spaces, actually. I'd probably use: $str = split(',[ ]*', $str); That way if you forget a space, it'll still work. Also, why do you have href="url=something"? That makes little sense. Link to comment https://forums.phpfreaks.com/topic/116732-transform-string/#findComment-600238 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 gonna assume it's used as a controller or switch Link to comment https://forums.phpfreaks.com/topic/116732-transform-string/#findComment-600240 Share on other sites More sharing options...
DarkWater Posted July 26, 2008 Share Posted July 26, 2008 Let's say you were on http://www.example.com/index.php. A link formatted as such: <a href="url=house">house</a> Would then lead you to: http://www.example.com/url=house So I think he needs to fix that. =P Link to comment https://forums.phpfreaks.com/topic/116732-transform-string/#findComment-600241 Share on other sites More sharing options...
MFHJoe Posted July 26, 2008 Share Posted July 26, 2008 I was under the impression that he was just going to replace 'url' with his actual URL once he got the code... Link to comment https://forums.phpfreaks.com/topic/116732-transform-string/#findComment-600246 Share on other sites More sharing options...
discomatt Posted July 26, 2008 Share Posted July 26, 2008 Comma and spaces, actually. I'd probably use: $str = split(',[ ]*', $str); That way if you forget a space, it'll still work. Also, why do you have href="url=something"? That makes little sense. No need to use regex here, not complex enough... explode and trim will be quite a bit quicker. Link to comment https://forums.phpfreaks.com/topic/116732-transform-string/#findComment-600251 Share on other sites More sharing options...
DarkWater Posted July 26, 2008 Share Posted July 26, 2008 Okay, fair enough, then he just needs to change the explode to: explode(',', $str); And leave out the trailing space. Link to comment https://forums.phpfreaks.com/topic/116732-transform-string/#findComment-600253 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.