LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2004, 02:25 PM   #1
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Rep: Reputation: 30
[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
 
Old 08-22-2004, 02:50 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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

Last edited by Cedrik; 08-22-2004 at 02:56 PM.
 
Old 08-22-2004, 03:25 PM   #3
Charalambos
Member
 
Registered: Aug 2004
Location: Switzerland
Distribution: debian
Posts: 149

Rep: Reputation: 15
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).

Last edited by Charalambos; 08-22-2004 at 03:27 PM.
 
Old 08-23-2004, 03:40 AM   #4
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
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.

Last edited by hylke; 08-23-2004 at 03:48 AM.
 
Old 08-23-2004, 03:52 AM   #5
Charalambos
Member
 
Registered: Aug 2004
Location: Switzerland
Distribution: debian
Posts: 149

Rep: Reputation: 15
You have to remove the spaces in the [ img] and [ /img] tag.

edit: (because the regular expression matches the tags without spaces).
 
Old 08-23-2004, 05:09 AM   #6
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
I've done that, but it simply removes those img tags, but i want only the url, and not all the other text.
Hylke
 
Old 08-23-2004, 05:11 AM   #7
Charalambos
Member
 
Registered: Aug 2004
Location: Switzerland
Distribution: debian
Posts: 149

Rep: Reputation: 15
Thats exactly what it's supposed to.
It you want more to be removed, add the desired match to the regular expression.
 
Old 08-23-2004, 05:27 AM   #8
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
How do i do that?
Because i dont understand how to do that with all those /,*/ and other signs.
Thanx Hylke
 
Old 08-23-2004, 06:37 AM   #9
Charalambos
Member
 
Registered: Aug 2004
Location: Switzerland
Distribution: debian
Posts: 149

Rep: Reputation: 15
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).
 
Old 08-23-2004, 06:40 AM   #10
Charalambos
Member
 
Registered: Aug 2004
Location: Switzerland
Distribution: debian
Posts: 149

Rep: Reputation: 15
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).
 
Old 08-23-2004, 06:51 AM   #11
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Sorry, but what does substitute mean?
 
Old 08-23-2004, 06:54 AM   #12
Charalambos
Member
 
Registered: Aug 2004
Location: Switzerland
Distribution: debian
Posts: 149

Rep: Reputation: 15
Substitute = replace.
This means you just have to put the desired string instead of the one in the example.
 
Old 08-23-2004, 12:38 PM   #13
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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;


Last edited by Cedrik; 08-23-2004 at 12:39 PM.
 
Old 08-23-2004, 01:58 PM   #14
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Thanx
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bootdisk.img mithu_sree Linux From Scratch 2 05-13-2005 03:50 AM
What's an initrd.img? hotel-lima Linux - Newbie 2 06-11-2004 05:50 PM
.img? dwessell Linux - Software 3 05-21-2004 09:06 AM
.img burner or way to boot from a .img American Psycho Linux - Software 4 03-24-2004 06:40 AM
img onetimewhiner Linux - Software 1 09-07-2001 03:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration