Solved

fix URL titles

Posted on 2007-04-02
8
247 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
Comment Utility
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
Comment Utility
ah ok...
0
 
LVL 16

Author Comment

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

im running PHP 5.4.1
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 16

Author Comment

by:ellandrd
Comment Utility
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
Comment Utility
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
Comment Utility
sorry - i didnt see the preg_replace in your first solution...  my eyes skipped over it...
0
 
LVL 16

Author Comment

by:ellandrd
Comment Utility
thank you BTW!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now