473,668 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace contents of href attribute in php

1 New Member
I have a php variable where the html contents are read as a string in php and I want to remove just the contents of the href and be the other intact.

$string = '<a href="javascrip t:#one">String</a>';
now I want to make this as given below.

<a href="https://bytes.com/">String</a>. Please note that javascript#one is not constant, It is dynamic
Feb 21 '20 #1
2 7675
gits
5,390 Recognized Expert Moderator Expert
you might use the preg_replace method for such a task.
Feb 21 '20 #2
Vanisha
25 New Member
To replace the contents of the href attribute in PHP, you can use string manipulation functions like str_replace or regular expressions.

Here is an example using str_replace:
<?php
$link = '<a href="https://example.com">Li nk</a>';
$new_url = 'https://newurl.com';
$new_link = str_replace('ht tps://example.com', $new_url, $link);
echo $new_link;
?>
also do PHP online course
In this example, the str_replace function is used to replace the old URL (https://example.com) with the new URL (https://newurl.com). The resulting string is stored in the $new_link variable and then output to the browser using echo.
Feb 16 '23 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1270
by: Robert Mark Bram | last post by:
Hi All! I have a menu system on my asp pages that use tables with a bunch of cells like this: <td class="menuHeader"><a href="../default.asp">Home</a></td> <td class="menuEntry"><a href="../blog/default.asp">Blog!</a></td> <td class="menuEntry"><a href="linkOMatic/default.asp">Link-O-Matic</a></td> Now I want a system where one of the cells has a different class if that page is being viewed. So, if default.asp is being viewed, that...
7
9206
by: Weston C | last post by:
I'm trying to get the href attribute from the base tag in the document head. The following does not seem to work... any ideas what I'm doing wrong? if(!(document.getElementsByTagName) || !(basehrefs = document.getElementsByTagName('base')) || !(basehrefs.length) || !(basehrefs.length > 0) || !(basehrefs.getAttribute) || !(basehref = basehrefs.getAttribute('href')) )
1
1873
by: Bernd Liebermann | last post by:
Hi, I need some help on a simple question: I have a link that should open a new window (no action in main window), which is realized by a function that calls window.open(). The function itsself works fine, but here is the problem: 1) If I use <A HREF="openToolWindow()">LinkText</A> then the plain source code of my function is loaded into the window.
4
26646
by: Jamie Jackson | last post by:
I know that it's possible to return false in an onClick to disable a link. Is there a way to do this within the HREF attribute? Something like: <a href="javascript: return false;"> ? Thanks, Jamie
2
2847
by: Gabriel Lozano-Morán | last post by:
Is there a reason why this does NOT work in IE6.0 ? <html> <head> <style type="text/css"> a { text-decoration: none; } a:hover
4
1761
by: dkacher | last post by:
Hello - I'm looking for a way to retain newlines when using XSL to copy the content of an XML attribute. I have this XML input: <?xml version="1.0"?> <A > <B x = "stuff" y = "line 1 line 2" /> </A>
17
30590
by: biju | last post by:
It is very common to see an image as a link on many web pages. Authors create them by putting an <imgtag inside a <atag. This is very much extra use of another tag. But why browsers dont support href attribute for <imgtag? Whether at any time this came up at W3C?
3
1689
thatos
by: thatos | last post by:
Is the any other way to remove contents of a stack without using the following loop or is there any other way to remove stack contents? Here is the loop Stack s = new Stack(); . . . . . while (s.empty == false){
3
2488
by: junkchiu | last post by:
want to remove "content" between two corresponding tags. What's the best way of doing it? I want to remove contents between <SEC-HEADER> and <table> tags. I have different tags, and I want to remove contents from html code, and what is the best way of scanning the file just once. I know I can count the number of lines in between but it deosn't seem to be efficeint. Thanks, <SEC-HEADER>0001047469-08-001731.hdr.sgml : 20080226 MAIL...
2
2563
by: harshchaturvedi | last post by:
sir,please tell me how can i put more then one picture in (href attribute)in html using notepad.
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.