LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [php]Reading url [img] and [/img] (https://www.linuxquestions.org/questions/programming-9/%5Bphp%5Dreading-url-%5Bimg%5D-and-%5B-img%5D-220973/)

hylke 08-22-2004 02:25 PM

[php]Reading url [img] and [/img]
 
Hello
Im writing a script language that looks like bbcode, so far, i did evrything with str_replace.
But i also need pictures, so i have to read text between [img ] and [/img ](minus those spaces).
So how do i do that?I hope sb can help me.
thanx Hylke

Cedrik 08-22-2004 02:50 PM

Try preg_replace() like :

PHP Code:

$text "Im writing a script language that looks like 
        bbcode [ img]http://image.com/smiley.png[ /img] so far,
        i did evrything with"
;

$img preg_replace("/.*\\[img\\](.*)\\[\\/img\\].*/""\\\\1"$text);
echo 
$img

edit : hard to figure out how to display img markup properly ;)

Charalambos 08-22-2004 03:25 PM

If you're not used to regular expression you can do something like this (not very clean solution though, i'd prefer reg expressions):
Code:

$newstring = strtr ( $inputstring, array("[ img ]" => "", "[ /img ]" => "") )
This will replace in inputstring the [ img ] and [ /img ] with an empty string (= remove it).
Like this you have in newstring just the content between the tags.

But nevertheless, if you really want to make it in a clean and much more elegant way, i suggest you to learn regular expressions (sooner or later you won't get around them anyway).

hylke 08-23-2004 03:40 AM

Thanks you all
Hylke

EDIT:
The code:
PHP Code:

 <?php
$text 
"Im writing a script language that looks like
        bbcode [ img]http://image.com/smiley.png[ /img] so far,
        i did evrything with"
;

$img preg_replace("/.*\[img\](.*)\[\/img\].*/""\\1"$text);
echo 
$img;
?>

Gives me the following output:
Quote:

Im writing a script language that looks like bbcode [ img]http://image.com/smiley.png[ /img] so far, i did evrything with
So i gues there's something wrong with the code.

Charalambos 08-23-2004 03:52 AM

You have to remove the spaces in the [ img] and [ /img] tag.

edit: (because the regular expression matches the tags without spaces).

hylke 08-23-2004 05:09 AM

I've done that, but it simply removes those img tags, but i want only the url, and not all the other text.
Hylke

Charalambos 08-23-2004 05:11 AM

Thats exactly what it's supposed to.
It you want more to be removed, add the desired match to the regular expression.

hylke 08-23-2004 05:27 AM

How do i do that?
Because i dont understand how to do that with all those /,*/ and other signs.
Thanx Hylke

Charalambos 08-23-2004 06:37 AM

Thats what i told you, you should learn how regular expressions work and how their syntax is in order to get that one straight. Thats why i showed you the version above avoiding regular expressions (in case you don't want to learn).

Charalambos 08-23-2004 06:40 AM

And btw, you just have to take Cedrik's example and substitute the string that is replaces those tags, and you have what you're looking for (if not, i didn't understand what you were looking for, and you have to tell me again).

hylke 08-23-2004 06:51 AM

Sorry, but what does substitute mean?

Charalambos 08-23-2004 06:54 AM

Substitute = replace.
This means you just have to put the desired string instead of the one in the example.

Cedrik 08-23-2004 12:38 PM

In my system the regex give only the url, nothing more... try again (replace [image] by [img])

$text = "some words before [image]http://site.com/image.png[/image] some words after...";

$url = preg_replace("/.*\[image\](.*)\[\/image\].*/", "\\1", $text);
$echo $url;


hylke 08-23-2004 01:58 PM

Thanx


All times are GMT -5. The time now is 08:34 AM.