?
Solved

fix URL titles

Posted on 2007-04-02
8
Medium Priority
?
253 Views
Last Modified: 2007-04-02
what im doing is creating a script that allows users to create HREFs.  they type in a string, select some word(s) and hit a button and my script inserts <a href="" and title=""></a> wrapped around selected word(s).

example - my string looks like this:

<p>this is a link to <a title="google" href="http://www.google.co.uk">google</a> and this is a link to <a title="google" href="http://www.quest-recruiting.com">quest</a></p>

as you can see, my second url title is google again.  i got help from hernst42 with extracting all the href title values from a string and i have these stored in an array called $titles.

so what i did next was this:

foreach($titles[1] as $title)
{
      $data = str_replace('title=""','title="'.$title.'"',$data);
}

but it isnt correct either.  how would you suggest i fix this?  basically i want to get the value between all the <a></a>'s in the string and insert that value into the correct <a title="" href=""></a>...
0
Comment
Question by:ellandrd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 18835454
If you go through all titles in the same sequence use
$data = str_replace('title=""','title="'.htmlentities(strip_tags($title)).'"',$data,1);
so only the first occurence of the  title="" is replaced and not all. and for the second match the second found title="" is replaces, ....
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18835475
ah ok...
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18835956
if i add the 1 to the end, my PHP wont run...

im running PHP 5.4.1
0
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 18836209
ok I've missunderstood the docu there. The count must be a variable which returns the numer of replacements. So try
$data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($title)).'"',$data,1);
which has a limit argument
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18836834
have you got an example working as i still cant get mine working:

$data = 'this is <a title="" href="">link1</a> and this is <a title="" href="">link2</a>';

preg_match_all('/<a[^>]+>(.*?)<\/a>/',$data,$titles);

$count = 1;

foreach($titles[1] as $title)
{
      $data = str_replace('title=""','title="'.htmlentities(strip_tags($title)).'"',$data,$count);
}

echo $data;
?>
0
 
LVL 48

Expert Comment

by:hernst42
ID: 18836904
This worked for me:
$data = 'this is <a title="" href="">link1</a> and this is <a title="" href="">link2</a>';
preg_match_all('/<a[^>]+>(.*?)<\/a>/',$data,$titles);
$count = 1;
foreach($titles[1] as $title)
{
    $data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($title)).'"',$data,$count);
}
echo $data;

the proble is that str_replace does not have a limit option, but preg_replace has a limit option.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18836928
sorry - i didnt see the preg_replace in your first solution...  my eyes skipped over it...
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18836930
thank you BTW!
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question