views:

44

answers:

1

these word1 and word2 is in brackets

want to remove the whole line depends on word2

[word1] something-line-text [word2] some text again

want to replace some text with another depends on word2

[word1] something-line-text [word2] some text again

into

REPLCACEDTEXT something-line-text some text again

some line text (something/something)

into

(something/something) some line text
A: 

I'm not 100% sure what you want, but this will look for [word1] and [word2] and remove them only if both are on the line:

$line = preg_replace('/word1(.*)word2/', '$1', $line);

This method could match on partial words, and you could be left with extra whitespace. Slightly better would be:

$line = preg_replace('/\s?word1\b(.*)\s?word2\b/', '$1', $line);

This makes sure that they are matched as whole words using word boundaries and spaces. I use spaces on one side so that the extra space is consumed. If you don't care about that just use \b on both sides of each word.

Second case, moves the first parenthesized phrase to the beginning of the line:

$line = preg_replace('/(.*)(\(.*?\))/', '$2 $1', $line);

Edit:

After seeing your example, I think you want:

$line = preg_replace('/^\[Scene\].*/', '', $line);

$line = preg_replace('/\[\.*?\]\s+(.*)(\[.*?\])/', '$2 $1');

$line = preg_replace('/(.*)\s+(\(.*?\))/', '$2 $1');
Jeff B
thanks first one works correctly but output is like-- [] sometext [] -- i want to remove the last bracket too, or both ones ,like -- [] somtextonly---
MIkeyy
Then include your brackets in the expression. `$line = preg_replace('/\s?\[word1\](.*)\s?\[word2\]/', '$1', $line);`
Jeff B
hmm its not working, what is there's ---[hey you] sometext [you]----
MIkeyy
hey its just working for first match only, what about other ones and its output remover first bracket not last one
MIkeyy
You lost me. If your text has spaces you have to use `\s`: `\[hey\syou\]`. I am not understanding your other question. Can you post an *actual* example, and what it returns?
Jeff B
acttually this is working with first match only, second matches remains same
MIkeyy
You have multiple matches on the same line? Or are you running this on a multi-line string?
Jeff B
there are lots of lines to same matches , multi lines
MIkeyy
If all of the lines are in the variable, you want: `$lines = preg_replace('/\s?\[word1\]([^\n\r]*)\s?\[word2\]/s', '$1', $lines);`
Jeff B
acctually its array and that one is also working on first match, and output is like ,$-- sometexttsdsds [word2] --- word1 is removed and word2 is still there
MIkeyy
Did you make sure to escape your brackets? `\[` Can you post what you are currently using for your preg_replace?
Jeff B
http://pastebin.com/d48c49899 check it
MIkeyy
See edits in answer.
Jeff B
nothing works :(
MIkeyy
what will be the regex for this 06:55:59-<kkksks>
MIkeyy