?
Solved

fix URL titles

Posted on 2007-04-02
8
Medium Priority
?
257 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

864 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