Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2004, 09:14 PM
Authorized User
 
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to insert links

Hello,

I work on content management system where a user can post articles on the web. I have php4, mysql4 and IIS

The question is: how is it possible to let user not only insert plain text, but also insert links to web references?
Similar to the way when I submit this http://p2p.wrox.com it should become highlighted as a link.

Thank you,


 
Old July 22nd, 2004, 09:30 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Try this:

<?php

//get what the user posted
$message = $_POST['message'];

//split the words into an array
$words = explode(" ",$message);

for($i = 0; $i < count($words); $i++)
{
    if(substr($words[$i],0,7)=="http://" || substr($words[$i],0,8)=="https://")
        $words[$i] = "<a href='$words[$i]'>$words[$i]</a>";
}

HTH,

Snib

<><
 
Old July 23rd, 2004, 01:38 PM
Authorized User
 
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you,
It works.

 
Old July 23rd, 2004, 03:06 PM
Authorized User
 
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, it works as it highlights the address.
But, there is a problem now when there is more that one link in the text field.
For example:

Input:
LIST OF SOURCES:
1. http://www.media-awareness.ca
2. See info at http://p2p.wrox.com

Print:
LIST OF SOURCES:
1. http://www.media-awareness.ca<BR< a> /> 2. See info at http://p2p.wrox.com<BR< a> /> (with one link to both addresses)

My code is:
//I take text from db field
$fieldg = nl2br(htmlspecialchars($row['field_g']));

$words = preg_split("/[\s,]+/",$fieldg);
for($i = 0; $i < count($words); $i++)
{
    if(substr($words[$i],0,7)=="http://" || substr($words[$i],0,8)=="https://"){
       $words[$i] = "<a href='$words[$i]'>$words[$i]</a>";
        }
}
//convert array to string
$strbody7 = implode(" ", $words);
    echo $strbody7;


I tried to remove nl2br(htmlspecialchars from line
$fieldg = nl2br(htmlspecialchars($row['field_g']));

then it gives me the following unformatted output:
LIST OF SOURCES: 1. http://www.media-awareness.ca 2. See info at http://p2p.wrox.com


Could you please help to solve this problem with correct formatting output of links?

Thank you,


 
Old July 23rd, 2004, 03:57 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

That's weird.... try putting the nl2br function after the link-creating code.

Not sure if this will change anything....

HTH,

Snib

<><





Similar Threads
Thread Thread Starter Forum Replies Last Post
trigger to insert current date on insert kev_79 SQL Server 2000 3 January 23rd, 2006 05:58 PM
List of Links chrome47 BOOK: Professional CSS: Cascading Style Sheets for Web Design 2 December 12th, 2005 10:37 PM
Sub links problem hlchuah77 CSS Cascading Style Sheets 3 February 2nd, 2005 02:21 PM
Generate links darkocobe Classic ASP Basics 1 October 15th, 2004 09:18 AM
Broken Links kilika Forum and Wrox.com Feedback 1 September 15th, 2003 10:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.