Jump to content

Hello everybody ... one year old problem


crnaovca

Recommended Posts

For a year or so I have a big problem I have to solve, I HAVE to solve, but still don't know how. Problem at first was in a approach to the problem, and now it is in regex part.

 

Since I am not good at regex at all, and I actually need it only for this one problem, please help me to solve this.

 

In CMS I have WYSWYG editor that gives me source of text like this

 

<p> Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text

 

Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text

 

<img alt="" src="http://www.site.com/v3/pics/Tuning_1.jpg" style="width: 400px; height: 300px;" /></p>

<p>

Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text

 

    <img alt="" src="http://www.site.com/v3/pics/Tuning_2.jpg" style="width: 400px; height: 300px;" />

 

Some text Some text Some text Some text Some text Some text Some text Some text

 

problem is that I have to change src part to add phpthumb.php in front (full url to phpthumb, not only file name) and in the end of src I have to put "width=400" or similar, 99% width AND must remove style and alt tag ...

 

So solution I have come up is that I have to find somehow all src URLs and then to remove all IMG tags and find replace them with new img tags I create as I want to.

 

Anyone have better idea? Or idea on how to do this I imagined? problem in the end is in whywyg editor that gives me something I cannot change easily. I thing regex would be great to do this and most elegant way on doing it.

 

Problem could be in that I am not sure if img tag look like this

 

    <img alt="" src="http://www.site.com/v3/pics/Tuning_2.jpg" style="width: 400px; height: 300px;" />

 

or

 

    <img src="http://www.site.com/v3/pics/Tuning_2.jpg" alt=""  style="width: 400px; height: 300px;" />

 

or

 

    <img alt=""  style="width: 400px; height: 300px;" src="http://www.site.com/v3/pics/Tuning_2.jpg"/>

 

or somehow else ...

 

-------

 

to short this up ...

 

how to find all URLs and find replace all img tags with new tags I create as I want to ...

 

(similar topic was here http://www.phpfreaks.com/forums/index.php?topic=229756.0 but since I do not know regex, I don't know how to change this)

Link to comment
Share on other sites

OK, I read this post twice and then took a look at the previous post. In the previous post the very first response stated:

I'm not sure I follow.. you haven't provided a before AND after sample...(no offense..most people do not know how to accurately describe what they are looking for.. that is why it is always advisable to show a sample of what you start with [which you did], and what you want it to be afterwards..

 

Looking at the post above, where exactly do you show the after of what you want? I have no idea how to interpret this statement:

problem is that I have to change src part to add phpthumb.php in front (full url to phpthumb, not only file name) and in the end of src I have to put "width=400" or similar, 99% width AND must remove style and alt tag ...

Link to comment
Share on other sites

The following will replace the src attribute in all image tags with "http://www.mysite.com/phpthumb.php". I think you actually want more than that, but you're going to have to do a better job of explaining/showing examples if you need more.

 

$output = preg_replace("#(<img [^>]+src=\")([^\"]+)#", '${1}http://www.mysite.com/phpthumb.php', $input);

Link to comment
Share on other sites

//Replace src attribute
$text = preg_replace("#(<img[^>]+src=[\"|'])([^\"|']+)#", '${1}http://www.mysite.com/phpthumb.php', $text);
//Remove style attribute (if exist)
$text = preg_replace("#(<img[^>]+?)(style=[\"|'][^\"|']+[\"|'])#", '${1}', $text);
//Add custom style attribute
$text = preg_replace("#(<img )([^>]+)#", '${1}style="width:400px;" ${2}', $text);

Link to comment
Share on other sites

Maybe problem is that English is not my native language so syntax is wrong.

 

Here is before example:

 

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

 

<img alt="something" src="http://www.somesite.com/images/image1.jpg" style="width: 400px; height: 300px;" />

 

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco

 

<img alt="something" src="http://www.somesite.com/images/image2.jpg" style="width: 400px; height: 300px;" />

 

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco

 

And here is what I want to get:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

 

<img alt="name of site insted of default" src="http://www.site.com/phpthumb.php?http://www.somesite.com/images/image1.jpg?w=400" />

 

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco

 

<img alt="name of site insted of default" src="http://www.site.com/phpthumb.php?http://www.somesite.com/images/image2.jpg?w=400" />

 

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco

 

And I want it on all IMG tags in text so all pictures will be same width with doesn't matter wht user uploaded.

Link to comment
Share on other sites

The problem was you didn't give an example of what you wanted the output to be. The above explains the changes for src. But, what is this supposed to mean:

 

alt="name of site insted of default"

 

 

Where does the name of the site come from???

Also, you stated you wanted the images to have a width attribute. But in your example above you ahve removed the style attributes and appended (incorrectly) a "?w=400". So, is the 400 supposed to be appended to the URL (correctly with an ampersand) or do you need the attribute set in the img tag?

 

The code below will replace the source as specified above (without the w=400).

 

//Replace src attribute$text = preg_replace("#(<img[^>]+src=[\"|'])([^\"|']+)#", '${1}http://www.mysite.com/phpthumb.php?${2}', $text);//Remove style attribute (if exist)$text = preg_replace("#(<img[^>]+?)(style=[\"|'][^\"|']+[\"|'])#", '${1}', $text);//Add custom style attribute$text = preg_replace("#(<img )([^>]+)#", '${1}style="width:400px;" ${2}', $text);

 

Link to comment
Share on other sites

<?php$test = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.<img alt="something" src="http://www.somesite.com/images/image1.jpg" style="width: 400px; height: 300px;" />Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco<img alt="something" src="http://www.somesite.com/images/image2.jpg" style="width: 400px; height: 300px;" />Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco';$out = preg_replace('~<img [^>]*src="(http://([^"/]+)[^"]+)"[^>]*>~', '<img alt="$2" src="http://www.site.com/phpthumb.php?$1?w=400" />', $test);echo $out;?>

 

Link to comment
Share on other sites

i tried this last code above and nothing happened ?? when I go to view image link, I get normal link, not modified src (i changed test with my text)

 

this ?w=400 is something that must be append to URL and I have to strip style part of tag, because I want phpthumb to modify picture width, and not to do it through style tag ...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.