unxposed Posted November 1, 2008 Share Posted November 1, 2008 I have many stings being created on my site such as: <a href="some/unknown/path/here.file">unknown_filename.file</a> But I want to replace the 'unknown_filename.file' between the a tags with 'Download', but keep everything else. $old_string = '<a href="some/unknown/path/here.file">unknown_filename.file</a>'; $new_string = preg_replace("/(>)(.*)(</a>)/", ">Download</a>", $download_old); This is as far as I've got... but it's obviously not working at all. Could anybody help me? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/131012-solved-replace-string-unknown-whilst-maintaing-another-unknown/ Share on other sites More sharing options...
unxposed Posted November 1, 2008 Author Share Posted November 1, 2008 I suppose I only actually need a way to get the link in the anchor tags... but still don't know how to do this ??? Quote Link to comment https://forums.phpfreaks.com/topic/131012-solved-replace-string-unknown-whilst-maintaing-another-unknown/#findComment-680168 Share on other sites More sharing options...
thebadbad Posted November 1, 2008 Share Posted November 1, 2008 Your regex is almost right (for your first request). You should make the .* match ungreedy by adding a question mark, and escape the slash, since that's what you're using for pattern delimiters. Also, you won't need the parentheses. />.*?<\/a>/ Quote Link to comment https://forums.phpfreaks.com/topic/131012-solved-replace-string-unknown-whilst-maintaing-another-unknown/#findComment-680178 Share on other sites More sharing options...
unxposed Posted November 1, 2008 Author Share Posted November 1, 2008 Thanks for that! It's a shame I don't think I need it anymore to get the file path though... or do I?. Quote Link to comment https://forums.phpfreaks.com/topic/131012-solved-replace-string-unknown-whilst-maintaing-another-unknown/#findComment-680221 Share on other sites More sharing options...
thebadbad Posted November 1, 2008 Share Posted November 1, 2008 If you want to store the paths (found in every href attribute in anchor tags), you can use this: <?php $string = '<a href="path1">text</a> another: <a href="path2">text</a>'; preg_match_all('~<a[^>]+?href="([^"]*)~i', $string, $matches); //have a look at found paths echo '<pre>', print_r($matches[1], true), '</pre>'; ?> Just let me know if you're trying to achieve something else. Quote Link to comment https://forums.phpfreaks.com/topic/131012-solved-replace-string-unknown-whilst-maintaing-another-unknown/#findComment-680236 Share on other sites More sharing options...
unxposed Posted November 1, 2008 Author Share Posted November 1, 2008 Great, I feel like I'm getting somewhere now. $download_string = $field->content; preg_match_all('~<a[^>]+?href="([^"]*)~i', $download_string, $matches); $download_link = $matches[0]; $field->content = '<a href="' . $download_link . '" class="download_link">Download</a>'; I'm still not getting anything an my href though. Can you spot anything silly? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/131012-solved-replace-string-unknown-whilst-maintaing-another-unknown/#findComment-680241 Share on other sites More sharing options...
wildteen88 Posted November 1, 2008 Share Posted November 1, 2008 You're original code was fine. It Just needed a few minor adjustments $old_string = '<a href="some/unknown/path/here.file">unknown_filename.file</a> hey man some more text <a href="some/here.file">ipppy kwoejmajnd.file</a> '; $new_string = preg_replace("/(>(.*)<\/a>)/i", ">Download</a>", $old_string); echo $old_string; echo '<hr/>'; echo $new_string; Quote Link to comment https://forums.phpfreaks.com/topic/131012-solved-replace-string-unknown-whilst-maintaing-another-unknown/#findComment-680242 Share on other sites More sharing options...
thebadbad Posted November 1, 2008 Share Posted November 1, 2008 Great, I feel like I'm getting somewhere now. $download_string = $field->content; preg_match_all('~<a[^>]+?href="([^"]*)~i', $download_string, $matches); $download_link = $matches[0]; $field->content = '<a href="' . $download_link . '" class="download_link">Download</a>'; I'm still not getting anything an my href though. Can you spot anything silly? Thanks again. $matches is a two dimensional array. If there's only one path you want to grab, as suggested in your code, try this: <?php $download_string = $field->content; preg_match('~<a[^>]+?href="([^"]*)~i', $download_string, $matches); $download_link = $matches[1]; $field->content = '<a href="' . $download_link . '" class="download_link">Download</a>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/131012-solved-replace-string-unknown-whilst-maintaing-another-unknown/#findComment-680259 Share on other sites More sharing options...
unxposed Posted November 1, 2008 Author Share Posted November 1, 2008 Thanks to both of you for your help... it's now working as desired with: $download_string = $field->content; preg_match_all('~<a[^>]+?href="([^"]*)~i', $download_string, $matches); $download_link = $matches[1][0]; $field->content = '<a href="' . $download_link . '" class="download_link">Download</a>'; Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/131012-solved-replace-string-unknown-whilst-maintaing-another-unknown/#findComment-680274 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.