简体   繁体   中英

Finding text and replacing it with Javascript

Currently I'm having some issues on my website where I made a noob mistake starting out and now it's becoming large and I am still using the out dated system I had in place when starting.

Basically, my website is using WordPress and my download links are stored within the_content portion on my site instead of in some form of custom meta, the worst part is they're not completely uniform only semi uniform which is where I'm having issues. ( I've searched most of stack, and other websites now for a source to fix my issue, and no luck )

Here's some of my code.

СКАЧАТЬ: 320 | 320 | 320 | iTunes | iTunes | iTunes
СКАЧАТЬ: CDQ | CDQ | CDQ | iTunes | iTunes | iTunes

Above is what's usually displayed in the_content portion on my website, it's always lead with СКАЧАТЬ: and followed by at least one link.

I need to replace this entire portion with <a href='download.php'>download</a> the rest is already coded, however my JavaScript knowledge is lacking.

EDIT: Not sure what you meant by update expected output code but here you go.

I want to have the content after СКАЧАТЬ: on my website replaced from the following, with the following.

Current:

<p style="text-align: center;"><strong>СКАЧАТЬ:</strong> <strong><a href="http://example.com">CDQ</a> | <a href="http://example.com">CDQ</a> | CDQ | <a href="http://example.com">iTunes</a> | <a href="http://example.com">iTunes</a> | iTunes</strong></p>

Maybe important, it's always wrapped around with a <p style="text-align: center;"> </p> tag also!

Expected:

<a href='download.php'>download</a>

If You wants output like this then this will help you else say what you want changes in this,

 (function(){ $('p.download-link a').fadeOut(function(){ $(this).html('<a href="download.php">download</a>').fadeIn(); }); })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p style="text-align: center;" class="download-link"><strong>СКАЧАТЬ:</strong> <strong><a href="http://example.com">CDQ</a> | <a href="http://example.com">CDQ</a> | CDQ | <a href="http://example.com">iTunes</a> | <a href="http://example.com">iTunes</a> | iTunes</strong></p> 

Hope this snippet will help you.

It seems to me like Javascript would be one way to approach this issue, but ultimately it would be ideal to have the content fixed in the database.

Have you thought about just trying to do a MySQL find/replace to fix the issue?

I have had a lot of luck using the Interconnect It PHP tool before. It comes highly recommended by many sources.

While you should definitely backup your database before doing a find/replace with any tool, part of the advantage of the Interconnect It tool is that it has a dry run feature you can run to see what would happen with your find/replace before actually making the changes in the database.

In this case I would try:

Replace

<p style="text-align: center;"><strong>СКАЧАТЬ:</strong> <strong><a href="http://example.com">CDQ</a> | <a href="http://example.com">CDQ</a> | CDQ | <a href="http://example.com">iTunes</a> | <a href="http://example.com">iTunes</a> | iTunes</strong></p>

With

<p class="download_link_class"><a href="download.php">download</a></p>

I'm guessing you will want to keep a paragraph tag around the replaced text and you may want to add a class to this new paragraph tag as well in case you want to style it differently... replace download_link_class above with your desired class name or just remove the class="" attribute entirely if you don't want it.

Again, make sure you back up the database before running this tool! If you have a separate (staging/dev) environment you can run this tool on first to actually see the results on a non-live site that would be ideal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM