Jump to content

[SOLVED] Replace string unknown, whilst maintaing another unknown


unxposed

Recommended Posts

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!

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

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.

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.

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;

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>';
?>

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!

Archived

This topic is now archived and is closed to further replies.

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