TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   How to take only the path? (http://www.talkphp.com/general/5615-how-take-only-path.html)

arsitek 10-30-2010 08:36 PM

How to take only the path?
 
I using CKFinder to let user browse the files from server to get path location of file direct to input form.

The problem is where the value of input form become like this:
PHP Code:

<p>
    <
img alt="" height="312" src="http://localhost/house/images/images/home-office-3-582x312.jpg" width="582" /></p

What PHP can do to clean that text input so I get only
PHP Code:

http://localhost/house/images/images/home-office-3-582x312.jpg 

to save on database

Thanks for help

Village Idiot 10-30-2010 10:55 PM

Quote:

Originally Posted by arsitek (Post 31161)
I using CKFinder to let user browse the files from server to get path location of file direct to input form.

The problem is where the value of input form become like this:
PHP Code:

<p>
    <
img alt="" height="312" src="http://localhost/house/images/images/home-office-3-582x312.jpg" width="582" /></p

What PHP can do to clean that text input so I get only
PHP Code:

http://localhost/house/images/images/home-office-3-582x312.jpg 

to save on database

Thanks for help

Lets take what we know about the string to see what we can do. We know:
1. It always will start with http://
2. It always ends with "

So basically what you need to do find the occurrence of every http:// and find the quote directly after it then take the contents between those positions. I haven't tested this, but something like the following should work

PHP Code:

<?
$off
=0;
$start="http://";
$end "\"";

while(
1)
{
    
//If a link is found in the text
    
if(stripos($haystack,$start,$off) !== false)
    {
        
//Set the temporary start and end positions
        
$startPos=stripos($haystack,$start,$off);
        
$endPos=stripos($haystack,$end,$location);
        
        
//Grab the current link
        
$link=substr($haystack,$startPos,$endPos-$startPos)
        
        
//Do what you want to the link here such as add it to an array
        
        //Set the offset to the end so it can search for the next link
        
$off=$endPos;
    }
    else
    {
        break;
    }
}
?>

Alternatively, if your code is always going to be well formed XHTML use an XML parser such as simpleXML.

arsitek 10-31-2010 05:00 AM

Thanks you very much Village Idiot :)

wGEric 11-03-2010 09:25 PM

A regular expression would work as well.

#<img.+src=(?:"|')(.*?)(?:"|').+/>#i

I haven't fully tested but that should match only in image tags and " or ' could be used.


All times are GMT. The time now is 09:10 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0