Solved

fix URL titles

Posted on 2007-04-02
8
249 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 48

Accepted Solution

by:
hernst42 earned 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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…

856 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