0
голосов
4ответов
3010 просмотров

How can I match the domain part of a URL in PHP?

I'm so bad at regexp, but I'm trying to get some/path/image.jpg out of http://somepage.com/some/...etc and trying this method: function removeDomain($string) { return preg_replace("/http:\/\/.*\//", "", $string); } It isn't working -- so far as I can tell it's just returning a blank stri...

0
голосов
4ответов
173 просмотров

Another preg_replace question!

Ive got a string that are words bunched together and I need to seperate them, and every word that ends in 'A' should probably be on a new line, item onea second itema third I also need to check if the word ending in 'A' should actually end in 'A' like extra or sultana. item oneasecond iteman...

0
голосов
1ответов
375 просмотров

Quickest way to Count the amount of numbers in a string, plus how to test PHP performance?

I need the best way of finding how many numbers in a string, would I have to first remove everything but numbers and then strlen? Also how would I go about testing the performance of a any PHP script I have written, say for speed and performance under certain conditions? UPDATE say I had to in...

0
голосов
3ответов
923 просмотров

preg_replace regular-expression to find numbers preceeded by letters

I'm really confused as to using preg_replace but slowly learning. I need help with the function: $str= preg_replace('#\W\d+#', '\W \d+', $str); The idea is that Im looking for numbers that have letters directly before them with no spaces, ie abc123. (NOT 'abc 123' and NOT '123abc') and how...

3
голосов
3ответов
438 просмотров

Wrapping a chunk of text with tags using PHP

I'm trying to allow a user (using Wordpress) to insert a jquery slideshow gallery (http://www.queness.com/resources/html/slideshow/jquery-slideshow.html) based on a faux tag. For example: [slideshow] <img src="url" /><br /> <img src="url" /> [!slideshow] Would produce...

15
голосов
4ответов
26114 просмотров

Simple preg_replace

I cant figure out preg_replace at all, it just looks chinese to me, anyway I just need to remove "&page-X" from a string if its there. X being a number of course, if anyone has a link to a useful preg_replace tutorial for beginners that would also be handy!

0
голосов
3ответов
2538 просмотров

Need help with simple preg_match/preg_replace regex in php

I'm trying to do a simple task of altering thousands of music video embed codes with a common with/height. For example, I have the following code: <object width="480px" height="407px" > <param name="allowFullScreen" value="true"/> <param name="wmode" value="transparent"/...

1
голосов
1ответов
369 просмотров

Using preg_replace to trim variable in PHP

I want to extract text from $_SERVER['HTTP_REFERER'] let's say $_SERVER['HTTP_REFERER'] = http://www.google.com/search?source=ig&hl=en&rlz=&q=something+i+am+looking+for&aq=f&oq=&aqi= then I want $query equal "something+i+am+looking+for". I figure I can use pregreplace ...

0
голосов
3ответов
641 просмотров

Regular expression to replace an <a> with respective <img></a>

I'm looking for a PHP preg_replace() solution find links to images and replace them with respective image tags. Find: &lt;a href="http://www.domain.tld/any/valid/path/to/imagefile.ext"&gt;This will be ignored.&lt;/a&gt; Replace with: &lt;img src="http://www.domain.tld/any/valid/path/to/image...

0
голосов
2ответов
4014 просмотров

Anchor tags to plain text within content

I am trying to match &lt;a&gt; tags within my content and replace then with the link text followed by the url in square brackets for a print-version. The following example works if there is only the "href". If the &lt;a&gt; contains another attribute, it matches too much and doesn't return the ...

1
голосов
1ответов
120 просмотров

Question about preg_replace in PHP

In PHP what is the difference between using \1 or $1 as $replacement in preg_replace()? They both work and seem to do the exact same thing, but I think I'm missing something here.

1
голосов
1ответов
293 просмотров

preg_replace easy for a pro

i want to find first select ... from and replace that only, following code replace all select..from in sql query, i just need for first select..from preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql);

3
голосов
5ответов
7205 просмотров

preg_replace() str_replace() apostrophe nightmare! - Drupal menu image replace

Can anyone help me decode why this doesnt work? $cssid = preg_replace("/'/", "", $cssid); Trying to strip the single quote marks from some html... Thanks! H EDIT This is the full function - it's designed to rebuild the Drupal menu using images, and it applies CSS classes to each item, allo...

12
голосов
2ответов
24978 просмотров

Using $ variables in preg_replace in PHP

Ummm... how do I use variables in a call to preg_replace? This didn't work: foreach($numarray as $num =&gt; $text) { $patterns[] = '/&lt;ces&gt;(.*?)$num(.*?)&lt;\/ces&gt;/'; $replacements[] = '&lt;ces&gt;$1&lt;$text/&gt;$2&lt;/ces&gt;'; } Yes, the $num is preceeded b...

1
голосов
1ответов
846 просмотров

Preg_replace solution for prepared statements

I have a command class that abstracts almost all specific database functions (We have the exactly same application running on Mssql 2005 (using ODBC and the native mssql library), MySQL and Oracle. But sometimes we had some problems with our prepare method that, when executed, replaces all placeh...

0
голосов
5ответов
1021 просмотров

RegEx string "preg_replace"

I need to do a "find and replace" on about 45k lines of a CSV file and then put this into a database. I figured I should be able to do this with PHP and preg_replace but can't seem to figure out the expression... The lines consist of one field and are all in the following format: "./1/024/9780...

0
голосов
2ответов
1165 просмотров

preg_replace: reference object in replacement

Do you know of any way to reference an object in the replacement part of preg_replace. I'm trying to replace placeholders (delimited with precentage signs) in a string with the values of attributes of an object. This will be executed in the object itself, so I tried all kinds of ways to refer to ...

5
голосов
7ответов
3597 просмотров

Minify CSS using preg_replace

I'm trying to minify multiple CSS files using preg_replace. Actually, I'm only trying to remove any linebreaks/tabs and comments from the file. the following works for me: $regex = array('{\t|\r|\n}', '{(/\*(.*?)\*/)}'); echo preg_replace($regex, '', file_get_contents($file)); But I'd like to...

3
голосов
2ответов
1366 просмотров

use preg_replace to replace character unless an escape character precedes

i'm trying to do the following hope there's a reg_ex expert around to shed some light. I need to replace the character [ in my code and make it a {. But there is cases where the [ needs to remain a [ and not change. So the way i figured it is i need to use the preg_replace("[", "{", $string); ...

2
голосов
4ответов
3152 просмотров

Regex to replace reg trademark

I need some help with regex: I got a html output and I need to wrap all the registration trademarks with a &lt;sup&gt;&lt;/sup&gt; I can not insert the &lt;sup&gt; tag in title and alt properties and obviously I don't need to wrap regs that are already superscripted. The following regex matche...

0
голосов
5ответов
1057 просмотров

regex: match string only if not part of a tag

I am trying to match a string only if it is not part of an html tag. For example when searching for the string: "abc". &lt;a href="foo.html"&gt;abc def&lt;/a&gt; should match &lt;p&gt; foo bar foo abc foo bar&lt;/p&gt; should match but &lt;a href="abc.html"&gt;foo&lt;/a&gt; should not match. ...

5
голосов
2ответов
32201 просмотров

PHP readdir and sort

I'm making a little gallery. I want to read the file names off a directory and print the file names below after I've stripped some leading numerals and file extensions. I have two versions of the code. Version 1 doesn't sort $current_dir = "$DOCUMENT_ROOT"."/weddings2/"; $dir = opendir($cur...

6
голосов
8ответов
38772 просмотров

PHP regular expression to remove tags in HTML document

Say I have the following text ..(content)............. &lt;A HREF="http://foo.com/content" &gt;blah blah blah &lt;/A&gt; ...(continue content)... I want to delete the link and I want to delete the tag (while keeping the text in between). How do I do this with a regular expression (since the U...

1
голосов
1ответов
361 просмотров

PCRE to replace #334455 hex with #345

I'm writing a function that replaces long hex coded color (#334455) with short one (#345). This can be only done when each color in hex is multiple of 17 (each hex pair consists of the same characters). e.g. #EEFFCC is replaced with #EFC, but #EDFFCC isn't replaced with anything. I want to make...

0
голосов
1ответов
402 просмотров

Help with preg_replace, stop at non alpa_numeric character

I need some help improving this function I made for parsing the links in a Twitter. It creates links for hashtags and @replys. It all works fine, the problem is if a hashtag or @reply has a punctuation character at the end with no space, it gets added to the HREF URL. For example if I Tweeted "I...

2
голосов
4ответов
3392 просмотров

PHP preg_replace() on multiple items

This is what I have so far: &lt;?php $text = preg_replace('/((\*) (.*?)\n)+/', 'awesome_code_goes_here', $text); ?&gt; I am successfully matching plain-text lists in the format of: * list item 1 * list item 2 I'd like to replace it with: &lt;ul&gt; &lt;li&gt;list item 1&lt;/li&gt; &lt;...

2
голосов
4ответов
2165 просмотров

preg_replace change img and link paths to use proxy

I've run into a hard problem to deal with. I am replacing a-tags and img-tags to fit my suggestions like this. So far so good. $search = array('|(&lt;a\s*[^&gt;]*href=[ Now my problem is that there are links on pages that i fetch the content of that looks like this: &lt;a href="/test/page/"&gt...

0
голосов
3ответов
334 просмотров

I have HTML comments being wrapped in Li and P tags :(

I have content that is first htmlentities and then stripslashes followed by nl2br. This means a watermark at the end ends up as: &lt;li&gt;&lt;p&gt;&lt;!-- watermark --&gt;&lt;/p&gt;&lt;/li&gt; Not very useful. I have the code below to try and strip the html comments and stop it displaying bu...

0
голосов
4ответов
337 просмотров

Replacing a string of text with another string using preg_replace

I have a string Action - [N]ew, [U]pdate, or [D]elete : N that I need to replace with "Action - [N]ew, [U]pdate, or [D]elete : U" somhow by using preg_replace I can't get it working. It remains the same. My code looks like this $action = Action - '[N]ew, [U]pdate, or [D]elete : U'; $line = preg...

1
голосов
2ответов
2047 просмотров

PHP preg_replace() backreferences used as arguments of another function

I am trying to extract information from a tags using a regex, then return a result based on various parts of the tag. preg_replace('/&lt;(example )?(example2)+ /&gt;/', analyze(array($0, $1, $2)), $src); So I'm grabbing parts and passing it to the analyze() function. Once there, I want to do wo...